PAT1060. 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思路:本题思路就是去除前导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 }

时间: 2024-08-14 01:58:43

PAT1060. Are They Equal的相关文章

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: 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 flo

[LeetCode] Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remem

com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user

1.错误描写叙述 553 Mail from must equal authorized user com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1333) at com.sun.mail.smtp.SMTPTransport.mail

Minimum Moves to Equal Array Elements

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

【LeetCode】462. Minimum Moves to Equal Array Elements II

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1. You may assume the array's length is at most 10

报错:org.apache.jasper.JasperException: /index.jsp (line: 1, column: 17) equal symbol expected

现象:写了如下一个jsp文件,导入需要用到的两个包: 运行结果报错:org.apache.jasper.JasperException: /index.jsp (line: 1, column: 17) equal symbol expected at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41) at org.apache.jasper.compiler.ErrorDis

java中的equal函数和==

这道题是我注册德问的时候做到的一道简单的概念题目.这里主要借这道题帮大家区分一下在java中"=="和equal的区别: 记住一下两句话就足够了: 1."=="比较的是地址,如果两个对象指向内存中的同一块区域的时候,即地址相同,为true,否则为false 2.equal()比较的是对象的值.

Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum

Scalaz是由一堆的typeclass组成.每一个typeclass具备自己特殊的功能.用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上.scala这个编程语言借鉴了纯函数编程语言Haskell的许多概念.typeclass这个名字就是从Haskell里引用过来的.只不过在Haskell里用的名称是type class两个分开的字.因为scala是个OOP和FP多范畴语言,为了避免与OOP里的type和class发生混扰,所以就用了typeclas

Cannot resolve the collation conflict between &quot;SQL_Latin1_General_CP1_CI_AS&quot; and &quot;Latin1_General_100_CI_AS&quot; in the equal to operation.

ErrorMessage Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AS" in the equal to operation. 查看SQL Server的Collation SELECT SERVERPROPERTY ('Collation') Solution 1. 在安装SQL Server的时候