1060 Are They Equal (25 分)

1060 Are They Equal (25 分)
 

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


精度模拟题,挺有意思的,特别注意0.000这种情况,我就在这组踩了好久的坑。

3 0 0.000
YES 0.000*10^0

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 int n;
 5 string s,ss;
 6 
 7 string getsome(string s,int &pos){
 8     while(s.length() > 0 && s[0] == '0')
 9         s.erase(s.begin());
10     string st;
11     if(s[0] == '.'){
12         int i= 1;
13         for(; i < s.length(); i++){
14             if(s[i] == '0'){
15                 pos--;
16             }else{
17                 break;
18             }
19         }
20 
21         for(; i < s.length(); i++){
22             st += s[i];
23         }
24         if(st.length() == 0){
25             pos = 0;
26         }
27         
28     }else{
29         bool flag = true;
30         for(int i = 0; i < s.length(); i++){
31             if(s[i] != '.'){
32                 st += s[i];
33                 if(flag)
34                     pos++;
35             }else{
36                 flag = false;
37             }
38         }
39     }
40     while(st.length() > n){
41         st = st.substr(0,n);
42     }
43     while(st.length() < n){
44         st += "0";
45     }
46     return st;
47 }
48 
49 
50 int main(){
51     cin >> n >> s >> ss;
52     int pos1=0,pos2=0;
53     string s1 = getsome(s,pos1);
54     string s2 = getsome(ss, pos2);
55     if(s1==s2 && pos1==pos2){
56         cout <<"YES 0."<<s1<<"*10^"<<pos1<<endl;
57     }else{
58         cout <<"NO 0."<<s1<<"*10^"<<pos1<<" 0."<<s2<<"*10^"<<pos2<<endl;
59     }
60     return 0;
61 }






原文地址:https://www.cnblogs.com/zllwxm123/p/11191401.html