您的位置:首页 > 其它

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

2017-08-09 19:24 218 查看


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);
else
w1=a.length();

if (w2!=-1)
b=b.substr(0,w2)+b.substr(w2+1,b.length()-w2+1);
else
w2=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;
}
else
a=a.substr(c1,a.length()-c1+1);

if (c2==-1)
{
b="0";
w2=0;
}
else
b=b.substr(c2,b.length()-c2+1);

if (a.length()<n)
for (int i=a.length();i<n;i++)	a=a+'0';
else
a=a.substr(0,n);

if (b.length()<n)
for (int i=b.length();i<n;i++)	b=b+'0';
else
b=b.substr(0,n);

if (w1==w2 && a==b)
cout<<"YES 0."<<a<<"*10^"<<w1;
else
cout<<"NO 0."<<a<<"*10^"<<w1<<" 0."<<b<<"*10^"<<w2;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: