1060 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 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 (<) 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 1, 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.d[1]...d[N]*10^k (d[1]>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

 

题意:

  用科学计数法表示两个浮点数,判断保留n位小数的两个数字是否相等。

思路:

  我们用cnta,cntb来表示两个浮点数的小数点的位置,pa, pb来表示两个数字第一个非0的位置。indexA[], indexB[] 用来表示结果的有效位数,expA, expB用来表示指数。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     int n;
 7     string a, b, tempA, tempB;
 8     cin >> n >> a >> b;
 9     int expA, expB;
10     int cntA = a.length(), cntB = b.length();
11     for (int i = 0; i < a.length(); ++i) {
12         if (a[i] == '.') {
13             cntA = i;
14             break;
15         }
16     }
17     for (int i = 0; i < b.length(); ++i) {
18         if (b[i] == '.') {
19             cntB = i;
20             break;
21         }
22     }
23 
24     int pA = 0, pB = 0;
25     while (a[pA] == '0' || a[pA] == '.') pA++;
26     while (b[pB] == '0' || b[pB] == '.') pB++;
27 
28     if (cntA < pA)
29         expA = cntA - pA + 1;
30     else
31         expA = cntA - pA;
32     if (pA == a.length()) expA = 0;
33 
34     if (cntB < pB)
35         expB = cntB - pB + 1;
36     else
37         expB = cntB - pB;
38     if (pB == b.length()) expB = 0;
39 
40     int indexA = 0, indexB = 0;
41     while (indexA < n) {
42         if (a[pA] != '.' && pA < a.length())
43             tempA += a[pA++];
44         else
45             tempA += '0';
46         indexA++;
47     }
48     while (indexB < n) {
49         if (b[pB] != '.' && pB < b.length())
50             tempB += b[pB++];
51         else
52             tempB += '0';
53         indexB++;
54     }
55     if (tempA == tempB && expA == expB) {
56         cout << "YES 0." << tempA << "*10^" << expA << endl;
57     } else {
58         cout << "NO 0." << tempA << "*10^" << expA << " 0." << tempB << "*10^"
59              << expB << endl;
60     }
61     return 0;
62 }

 

  这种题难道是不难,就是细节太多,有一点考虑不到,就会被设计好的样例卡到。(上面的代码有一组数据没有通过)

 

posted @ 2020-05-06 18:06  Veritas_des_Liberty  阅读(227)  评论(0编辑  收藏  举报