A1060. Are They Equal
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
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; // 0000.0012 0123.34 5 char str1[200], str2[200]; 6 int process(char s[], int len){ 7 int i,j, exp; 8 for(i = 0; s[i] != '\0' && s[i] == '0'; i++); //去除前导0 9 if(s[i] != '\0'){ 10 for(j = 0; s[j + i] != '\0'; j++) 11 s[j] = s[j + i]; 12 s[j] = '\0'; 13 }else{ 14 for(int k = 0; k < len; k++) 15 s[k] = '0'; 16 s[len] = '\0'; 17 return 0; 18 } 19 if(s[0] == '.'){ //.0012 20 for(i = 1; s[i] != '\0' && s[i] == '0'; i++); 21 if(s[i] == '\0'){ 22 exp = 0; 23 s[0] = '0'; 24 }else{ 25 exp = i - 1; 26 exp *= -1; 27 for(j = 0; s[i + j] != '\0'; j++) 28 s[j] = s[j + i]; 29 s[j] = '\0'; 30 } 31 }else{ //123.456 32 for(i = 0; s[i] != '\0' && s[i] != '.'; i++); 33 if(s[i] == '\0'){ 34 exp = i; 35 }else{ 36 exp = i; 37 for(; s[i + 1] != '\0'; i++) 38 s[i] = s[i + 1]; 39 s[i] = '\0'; 40 } 41 } 42 for(j = 0; s[j] != '\0'; j++); 43 while(j < len){ 44 s[j++] = '0'; 45 } 46 s[len] = '\0'; 47 return exp; 48 } 49 int main(){ 50 int N, exp1, exp2; 51 scanf("%d %s %s", &N, str1, str2); 52 exp1 = process(str1, N); 53 exp2 = process(str2, N); 54 int tag = 1; 55 for(int i = 0; str1[i] != '\0'; i++) 56 if(str1[i] != str2[i]){ 57 tag = 0; 58 break; 59 } 60 if(tag == 1 && exp1 == exp2){ 61 printf("YES 0.%s*10^%d", str1, exp1); 62 }else{ 63 printf("NO 0.%s*10^%d 0.%s*10^%d", str1, exp1, str2, exp2); 64 } 65 cin >> N; 66 return 0; 67 }
总结:
1、题意:将给出的两个数字变成保留指定位小数的科学计数法的数字,之后看其是否相等。
2、主要分为两种数字,大于1和小于1(0.000123, 1234.5678),其次要注意有0000123的情况出现,要先去除前导0.
3、主要要做的就是想办法求出不带小数点的且符合要求的底数。
4、测试样例: 3 0.0 0
输出:YES 0.000*10^0