PAT 1060. Are They Equal (25) 只做出19分。。

来源:互联网 发布:淘宝助理天猫版 编辑:程序博客网 时间:2024/04/30 15:28

1060. Are They Equal (25)

时间限制
50 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码


/*  太繁琐,很难全通过~~~~~~~~~~~~!!!形参变量 与 主函数变量 不要同名混淆,否则检查半天会很无语。!!!!注意程序中及时break!!  科学计数法 分为 整数 0 小数 三种情况讨论,注意补0 去0。特殊case:0021,0.0021,0,12.123*/#include<stdio.h>#include<string.h>const int MAX=200;//此处是测试点,设置为5,扣2分。。。char tmp[MAX];void init(char a2[],char b2[]){int i;for(i=0;i<MAX;i++){a2[i]=0;b2[i]=0;}}char* subStr(char m[],int p,int q,int r) //此处用(char a[],int i,int j,int w)老是出错,原来是形参变量i,j 与 主函数变量i,j 同名混淆。。{     // m[]表示一个数组,p表示开始截取的位置,q表示截取的长度,r表示整个数组的长度。         int i,k,size=0;  //由于涉及到字符串后面填充字符'0',故需要知道 数字总位数 bool isDecimal=0;for(i=0;i<MAX;i++)tmp[i]=0;for(k=p;k<q;k++){   //如果整数部分不够有效精度,那么在后面补0.if(m[k]=='.'||k>=r){tmp[size++]='0';isDecimal=1;}else if(isDecimal==0)tmp[size++]=m[k];elsetmp[size++]='0';}return tmp;}int main(){int n,i;char a[MAX],b[MAX],a1[MAX],b1[MAX];//freopen("G:\\in.txt","r",stdin);//freopen("G:\\our.txt","w",stdout);while(scanf("%d",&n)!=EOF){init(a,b);scanf("%s%s",a,b);int len1=strlen(a), len2=strlen(b);int cnt1=0,cnt2=0;for(i=0;i<len1;i++){  //计算第1个数的整数部分总位数if(a[0]=='0')break;else if(a[i]!='.')cnt1++;else          //扫到小数点就终止扫描!!!勿忘。。。。。break;}for(i=0;i<len2;i++){  //计算第2个数的整数部分总位数if(b[0]=='0')break;else if(b[i]!='.')cnt2++;else          //扫到小数点就终止扫描!勿忘。。。。。break;}//printf("初始串:%s %s\n",a,b);strcpy(a1,subStr(a,0,n,len1));strcpy(b1,subStr(b,0,n,len2));if(cnt1==cnt2){if(strcmp(a1,b1)==0)printf("YES 0.%s*10^%d",a1,cnt1);elseprintf("NO 0.%s*10^%d 0.%s*10^%d",a1,cnt1,b1,cnt2);}else{printf("NO 0.%s*10^%d 0.%s*10^%d",a1,cnt1,b1,cnt2);}printf("\n");}return 0;}

 

0 0
原创粉丝点击