【PAT】【Advanced Level】1060. Are They Equal (25)

来源:互联网 发布:php开发实战视频教程 编辑:程序博客网 时间:2024/05/21 10:26

1060. Are They Equal (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
原题链接:

https://www.patest.cn/contests/pat-a-practise/1060

https://www.nowcoder.com/pat/5/problem/4113

思路:

先处理小数点,再处理0,然后对齐,最后比较

花式if else

坑点:

0的处理

CODE:

#include<iostream>#include<cstring>#include<string>using namespace std;int main(){int n;string a,b;cin>>n>>a>>b;int w1=-1,w2=-1;for (int i=0;i<a.length();i++){if (a[i]=='.'){w1=i;break;}}for (int i=0;i<b.length();i++){if (b[i]=='.'){w2=i;break;}}if (w1!=-1)a=a.substr(0,w1)+a.substr(w1+1,a.length()-w1+1);elsew1=a.length();if (w2!=-1)b=b.substr(0,w2)+b.substr(w2+1,b.length()-w2+1);elsew2=b.length();int c1=-1,c2=-1;for(int i=0;i<a.length();i++){if (a[i]!='0') {c1=i;break;}w1--;}for(int i=0;i<b.length();i++){if (b[i]!='0') {c2=i;break;}w2--;}if (c1==-1){a="0";w1=0;}elsea=a.substr(c1,a.length()-c1+1);if (c2==-1){b="0";w2=0;} elseb=b.substr(c2,b.length()-c2+1);if (a.length()<n)for (int i=a.length();i<n;i++)a=a+'0';elsea=a.substr(0,n);if (b.length()<n)for (int i=b.length();i<n;i++)b=b+'0';elseb=b.substr(0,n);if (w1==w2 && a==b)cout<<"YES 0."<<a<<"*10^"<<w1;elsecout<<"NO 0."<<a<<"*10^"<<w1<<" 0."<<b<<"*10^"<<w2;return 0;}