码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1060. Are They Equal

时间:2015-02-16 16:44:53      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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
思路:本题思路就是去除前导0,而且在使用 str.erase()的时候,里面内置的参数必须是地址,切记。
技术分享
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int n;
 5 string Change(string str,int &e)
 6 {
 7     int k=0;
 8     while(str.length()>0&&str[0]==0)
 9     {
10         str.erase(str.begin());
11     }
12     if(str[0]==.)
13     {
14         str.erase(str.begin());
15         while(str.length()>0&&str[0]==0)
16          {
17             str.erase(str.begin());
18             e--;
19         }
20     }
21     else
22     {
23         while(k<str.length())
24         {
25             if(str[k]!=.)
26             {
27                 k++;
28                 e++;
29             }
30             else 
31             {
32                 str.erase(str.begin()+k);
33                 break;
34             }
35         }        
36     }
37     if(str.length()==0)
38     {
39         e=0;
40     }
41     string ans;
42     int i;
43     for(i=0;i<n;i++)
44     {
45         if(i<str.length())
46         { 
47             ans+=str[i];//string需要用+相加 
48         }
49         else
50             ans+=0;          
51     }
52     return ans;
53 }
54 int main(int argc, char *argv[])
55 {
56     string str1,str2;
57     cin>>n>>str1>>str2;
58     int e1=0,e2=0;
59     string ans1=Change(str1,e1);
60     string ans2=Change(str2,e2);
61     if(ans1==ans2&&e1==e2)
62     {
63         cout<<"YES 0."<<ans1<<"*10^"<<e1<<endl;
64     }
65     else
66     {
67         cout<<"NO 0."<<ans1<<"*10^"<<e1<<" 0."<<ans2<<"*10^"<<e2<<endl;
68     }
69     return 0;
70 }
View Code

 

PAT1060. Are They Equal

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4294218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!