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.9Sample Output 1:
YES 0.123*10^5Sample Input 2:
3 120 128Sample Output 2:
NO 0.120*10^3 0.128*10^3
备注:不难,就是要考虑的方面太多了。1)注意数字前多余的0要去掉;2)对于0的特殊处理;3)对于大于1的数和小于1大于0的数处理方法不同。刚开始时没有转换成科学计数法直接比较,只过了3个case;后来改成了转换完后直接比较,既方便又不容易搞错。
各种要求的计数格式:(有效位数 原数 要求的计数格式)
3 0 0.000*10^0
5 0.000000 0.00000*10^0
3 0.1 0.1*10^0
4 0.000010100 0.1010*10^-4
5 1.000201 0.10002*10^1
#include<stdio.h> #include<string.h> #define MAXSIZE 101 //judge whether the number is 0 int IsZero(char s[]) { if(s[0]=='0' && strlen(s)==1) // s="0" return 1; else { for(int i=0;s[i]!='\0';i++) if(s[i]!='0' && s[i]!='.') return 0; } return 1; // s="0.0000000" } //judge whether the number is less than 1, like 0.XXXXXXXXXXXX (0<X<=9) int IsLessThanOne(char s[]) { if(s[0]=='0' && s[1]=='.') return 1; return 0; } //judge whether the number is a float point int HasPoint(char s[]) { for(int i=0;s[i]!='\0';i++) { if(s[i]=='.') return 1; } return 0; } //chop the leading zero before the number, store the result in string t[] void ChopLeadingZeros(char s[],char t[]) { if(IsZero(s)) // s="0" or "0.000000000000" { strcpy(t,s); return; } if(s[0]!='0') // s="123" or "123.45" { strcpy(t,s); return; } int j=0, HasChopped=0; for(int i=0;s[i]!='\0';i++) { if(!HasChopped) { if((s[i]!='0') || (s[i]=='0' && s[i+1]=='.')) { t[j++]=s[i]; HasChopped=1; } } else t[j++]=s[i]; } t[j]='\0'; } // for a number >=1, Count the number of digits before "." // for a number <1, count the number of zeros before the first non-zero digit int CountBeforePoint(char s[]) { int count=0; if(IsZero(s)) // s="0" or "0.000000000000" return 0; if(IsLessThanOne(s)) // 0.XXXXXXX(0.1XXXXXX returns 0, 0.0003XXXXX returns 3 { for(int i=0;s[i]!='\0';i++) { if(s[i]=='0') count++; else if(s[i]!='0' && s[i]!='.') break; } return count-1; } for(int i=0;s[i]!='\0';i++) // 123000 or 123.45 { if(s[i]=='.') break; else count++; } return count; }
// transform a number to the standard form with k significant points // store the result in string t[] void TransformNum(char s[],int k,char t[]) { int count = CountBeforePoint(s); int u = 2; t[0] = '0'; t[1] = '.'; if(IsZero(s)) // s='0' { for(int i=0;i<k;i++) t[u++] = '0'; } else if(IsLessThanOne(s)) // handle 0.XXXXXXXXX { int j=0; for(int i=0;j<k && s[i]!='\0';i++) { if(j>0) { t[u++] = s[i]; j++; } else if(s[i]!='0' && s[i]!='.') { t[u++] = s[i]; j++; } } count = -count; } else if(s[0]!='0' && HasPoint(s) && k>count) // handle 1230.45 and k=5 { k++; for(int i=0;i<k && s[i]!='\0';i++) { if(s[i]!='.') t[u++] = s[i]; } } else { for(int i=0;i<k && s[i]!='\0';i++) { if(s[i]!='.') t[u++] = s[i]; } } sprintf(t+u,"*10^%d",count); } int main() { int N; char s1[MAXSIZE],s2[MAXSIZE]; char t1[MAXSIZE],t2[MAXSIZE]; char r1[MAXSIZE],r2[MAXSIZE]; scanf("%d %s %s",&N,s1,s2); // first delete the leading zeros before the number ChopLeadingZeros(s1,t1); ChopLeadingZeros(s2,t2); // transform the num to the required format TransformNum(t1,N,r1); TransformNum(t2,N,r2); // compare the transformed result if(strcmp(r1,r2)==0) printf("YES %s",r1); else printf("NO %s %s",r1,r2); return 0; }