PAT 1060 Are They Equal
来源:互联网 发布:js 跳转url 隐藏参数 编辑:程序博客网 时间:2024/05/19 13:23
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
题目大意:给出两个数,问:将他们写成保留 N 位小数的科学记数法后是否相等。如果相等,则输出 YES ,并给出该转换结果;如果不相等,则输出 NO ,并分别给出两个数的转换结果。
题目要求将两个数写成科学计数法的形式,然后判断它们两个是否相等。而科学计数法的形式一定是如下格式:0.a1a2a2.......*10^e, 因此只要获取到科学计数法的本体部分a1a2a3a4...和指数 e ,即可判定两个数在科学计数法下是否相等。
首先考虑数据本身,可以想到按整数部分是否为零来分情况讨论,即
(1) 0.a1a2a3......
(2)b1b2b3....bm . a1a2a3....
情况一:由于在小数点后还可能跟着 若干个 0 ,因此本体部分是从小数点后第一个非零位开始的,而指数则是小数点与该非零之间零的个数的相反数 (例如 0.001的指数为 -2).
情况二:假设 b1不为零,显然本体部分就是从 b1开始的,而指数就是 小数点前的总数位 m。
题目中隐含了一个trick:数据有可能出现前导零,即在(1)或者数据 (2) 之前还会有若干个 0 (例如:000.1或是 00123.45),为了应对这种情况,需要在输入数据的第一步就是去除所有的前导零,这样就可以按去除前导零后的字符串的第一位是否是小数点来判断其属于 (1)或是(2).
由于需要两个数的科学计数法进行比较,因此必须把各自的本体部分单独提取出来。比较合适的方法是,处理(1)时,将前导零,小数点,第一个非零位前的零全部删除,只保留第一个非零位开始的部分(即 ak,a(k+1)......).处理二时,将前导零,小数点删除,保留从b1开始的部分。
最后只要比较本体部分和指数是否相等,就可以决定输出YES还是NO.
代码如下:
#include<cstdio>#include<iostream>#include<string>using namespace std;int n;string deal(string s,int &e){ int k=0;/// while(s.length()>0&&s[0]=='0')///去掉s的前导零 s.erase(s.begin()); if(s[0]=='.') ///去掉前导零后是小数点,说明是小于零的数 { s.erase(s.begin()); ///去掉小数点 while(s.length()>0&&s[0]=='0') { s.erase(s.begin()); ///去掉小数点后非零位前的所有零 e--; ///每去掉一个零,指数e -1 } } else ///去掉前导零后不是小数点,则找到后面的小数点删除 { while(k<s.length()&&s[k]!='.')///寻找小数点 { k++; e++; ///只要不碰到小数点就让e++; } if(k<s.length())///while结束后k<s.length(),说明碰到了小数点 s.erase(s.begin()+k);///把小数点删除 } if(s.length()==0)///除去前导零和小数点后长度变为0,说明这个数是零 e=0; int num=1; k=0; string res; while(num<=n)///只要精度还没有到n,进行循环直到精度到n为止 { num++; if(k<s.length()) res+=s[k++];///字符串的拼接 else res+='0'; } return res;}int main(){ string s1,s2,s3,s4; cin>>n>>s1>>s2; int e1=0,e2=0; s3=deal(s1,e1);///返回字符串的有效位数 s4=deal(s2,e2); if(s3==s4&&e1==e2) cout<<"YES 0."<<s3<<"*10^"<<e1<<endl; else cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2<<endl; return 0;}
- PAT 1060 Are They Equal
- PAT 1060 Are They Equal?
- PAT 1060 Are They Equal
- PAT 1060 Are They Equal 暴力
- PAT 甲级 1060 Are They Equal
- PAT 1060. Are They Equal
- PAT 1060. Are They Equal
- PAT 1060. Are They Equal
- 【PAT】1060. Are They Equal
- PAT [A1060]-Are They Equal
- Pat(Advanced Level)Practice--1060(Are They Equal)
- PAT (Advanced Level) Practise 1060 Are They Equal (25)
- PAT (Advanced Level) Practise 1060 Are They Equal (25)
- PAT-1060 Are They Equal (科学计数法)
- 1060. Are They Equal (25)-PAT
- pat 1060. Are They Equal (25)
- PAT A 1060. Are They Equal (25)
- PAT 1060. Are They Equal (25)
- python异常与错误
- [bzoj4719][树链剖分][Noip2016]天天爱跑步
- TreeSet 集合在Add时报错cannot be cast to java.lang.Comparable
- [Err] 1062
- 网络编程(三)--TCP
- PAT 1060 Are They Equal
- Jenkins Centos部署过程中异常No route to host问题解决
- 使用gdb修改寄存器中的内容
- GBDT+LR特征融合的例子
- 习题 6.3 求一个3*3的整型矩阵对角线元素之和。
- 剑指Offer---从头到尾打印链表
- makefile编写
- HttpURLconnection_webview
- 获取应用程序当前工作目录的绝对路径