Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码

题目描述:

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and
two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:

Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100,
and that its total digit number is less than 100.

Output Specification:

For each test case, print in a line "YES" if the two numbers are treated equal, and then the number in the standard form "0.d1...dN*10^k" (d1>0 unless the number
is 0); or "NO" if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

AC代码:

浮点数的科学计数法,纯字符串的比较;

下面是几个测试用例:

1.有前导0的情况 例如:

3 00000012.3 0000001.1

NO 0.123*10^2 0.11*10^1(太坑,一点提示也没有)

2.还有就是当位数不足N位时,不需要补全 , 如上的 0000001.1

3.0的不同表示,如:

5 0000000 0000000.000000

YES 0.00000*10^0

其实这题有些搞笑, 如下:

4   00000012.0 0000012.00

NO 0.120*10^2 0.1200*10^2

同样是0.12不相等。。。  纯字符串比较,题目要求不删除小数部分的后导0

#include<cstdio>
#include<cstring>
#define MAX 105

using namespace std;

int Process(char str[],int n,char ret[])
{
	int i=0,j=0;
	int exp=0;
	while(str[i]==‘0‘)//去掉前面的0
		i++;
	if(str[i]==‘.‘)//型如0000000.000*****的浮点数
	{
		i++;
		while(str[i]==‘0‘)
		{
			i++;
			exp--;
		}
		if(str[i]==‘\0‘)//0的另一种形式如00000.000000
		{
			int k;
			exp=0;
			for(k=0;k<n;k++)
				ret[k]=‘0‘;
			ret[k]=‘\0‘;
			return exp;
		}
		while(str[i]!=‘\0‘)
		{
			ret[j]=str[i];
			i++;
			j++;
			if(j==n)
				break;
		}
	}
	else
	{
		if(str[i]==‘\0‘)//0的一种表示例如00000000
		{
			int k;
			exp=0;
			for(k=0;k<n;k++)
				ret[k]=‘0‘;
			ret[k]=‘\0‘;
			return exp;
		}
		else//0000*****.******
		{
			while(str[i]!=‘.‘&&str[i]!=‘\0‘)
			{
				if(j<n)//取n位的精度
				{
					ret[j]=str[i];
					j++;
				}
				i++;
				exp++;
			}
			if(j<n&&str[i]!=‘\0‘)
			{
				i++;
				while(str[i]!=‘\0‘)
				{
					ret[j]=str[i];
					i++;
					j++;
					if(j==n)
						break;
				}
			}
		}
	}
	ret[j]=‘\0‘;
	return exp;
}

int main(int argc,char *argv[])
{
	int N;
	char A[MAX],B[MAX];
	scanf("%d %s %s",&N,A,B);
	char retA[MAX],retB[MAX];
	int expA=Process(A,N,retA);
	int expB=Process(B,N,retB);
	if(expA==expB&&strcmp(retA,retB)==0)
		printf("YES 0.%s*10^%d\n",retA,expA);
	else
		printf("NO 0.%s*10^%d 0.%s*10^%d\n",retA,expA,retB,expB);

	return 0;
}

Pat(Advanced Level)Practice--1060(Are They Equal)

时间: 2024-11-06 07:47:57

Pat(Advanced Level)Practice--1060(Are They Equal)的相关文章

1002 A+B for Polynomials (PAT (Advanced Level) Practice)

This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?

PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi