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:
1 |
3 12300 12358.9 |
Sample Output 1:
1 |
YES 0.123*10^5 |
Sample Input 2:
1 |
3 120 128 |
Sample Output 2:
1 |
NO 0.120*10^3 0.128*10^3 |
最后一个测试点,当数字为 0 时,指数不能是 1
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include<stdio.h> #include<string.h> int handle(char *str,int n) { int first=-1,i,count,total; char *point,temp[105]; point=strchr(str,'.'); total=strlen(str); if(point==NULL) { point=str+total; } for(i=0;str[i];i++) { if(str[i]!='.'&&str[i]!='0') { first=i; break; } } if(first==-1) { str[0]='0'; str[1]='.'; for(i=0;i<n;i++) { str[i+2]='0'; } str[i+2]=0; return 0;//最后一个测试点有点不科学,当数字为 0 时,只按照那个 standard form 为什么指数不能是 1 ? } else { count=point-str-first; temp[0]='0'; temp[1]='.'; for(i=0;i<n;i++) { if(i+first>=total) { temp[i+2]='0'; } else if(str[i+first]=='.') { i--; first++; } else { temp[i+2]=str[i+first]; } } temp[i+2]=0; } strcpy(str,temp); if(count<0) { count++; } return count; } int main() { char a[105],b[105]; int n,power_a,power_b; scanf("%d%s%s",&n,a,b); power_a=handle(a,n); power_b=handle(b,n); if(power_a==power_b&&!strcmp(a,b)) { printf("YES %s*10^%d\n",a,power_a); } else { printf("NO %s*10^%d %s*10^%d\n",a,power_a,b,power_b); } } |