PAT Grade --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 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

其实这道题的难度在于得到这个数的幂次
. 1 #include <the iostream>
 2 #include < String >
 . 3  the using  namespace STD;
 . 4  String A, B; // Note that, outside the range 10 ^ 100 double and can access using character strings 
. 5  int N;
 . 6  int dealString ( String & STR) // data pre-processing, returns the number of data bits 
. 7  {
 . 8      int K = str.find ( ' . ' ;) // find the decimal point 
. 9      IF - (K =! . 1 )
 10      {
 . 11          str.erase (K , 1); // delete the decimal point 
12 is          IF (STR [ 0 ] == ' 0 ' )
 13 is          {
 14              K = 0 ;
 15              str.erase ( 0 , . 1 ); // delete the first 0 
16          }
 . 17      }
 18 is      the else / / no decimal 
. 19      {
 20 is          IF (STR =! " 0 " )
 21 is              K = str.length ();
 22 is          the else 
23 is         {
 24              K = 0 ;
 25              str.erase ( 0 , . 1 ); // delete the first 0 
26 is          }
 27      }
 28      the while (str.empty () && STR [! 0 ] == ' 0 ' )
 29      {
 30          str.erase ( 0 , . 1 ); // preceding output 0 
31 is          K--; // such as 0.128 * 10 ^ = 0.000128 -3 
32      }
 33 is      IF (str.empty ()) // this number is 0
34 is          K = 0 ;
 35      the while (str.length () < N)
 36          STR + = " 0 " ; // number of bits is not enough to scrape 0 
37 [      return K;
 38 is  }
 39  
40  int main ()
 41 is  {
 42 is      CIN >> >> >> a N B;
 43 is      // used k1, k2 to obtain a, B, bits 
44 is      int k1, k2;
 45      K1 = dealString (a);
 46 is      K2 = dealString (B);
 47      A.assign ( a.begin (), a.begin () + N); //取前N位
48     B.assign(B.begin(), B.begin() + N);
49     if (A == B && k1 == k2)
50     {
51         cout << "YES ";
52         cout << "0." << A << "*10^" << k1 << endl;
53     }
54     else
55     {
56         cout << "NO ";
57         cout << "0." << A << "*10^" << k1 << " ";
58         cout << "0." << B << "*10^" << k2 << endl;
59     }
60     return 0;    
61 }

 

Guess you like

Origin www.cnblogs.com/zzw1024/p/11294212.html