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.d[1]...d[N]*10^k
(d[1]
>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的,首先去除前导0化成xxx.xx...x的形式;再考虑有没有小数点,
有小数点的话再看小数点是否在第二位,即x.xx...x的形式,然后通过移位对指数
进行加减,最后去掉小数点(最后的结果都为0.xx..x,相当于全不加0.)。比较的时候
将指数和数值部分一起比较。
本题坑点主要有三个:前导0;0和0.0;最后数值位长度恒为n(不足要补0,多了要截断)
附上一些测试点:
4 0000 0000.00 YES 0.0000*10^0
4 00123.5678 0001235 NO 0.1235*10^3 0.1235*10^4
3 0.0520 0.0521 NO 0.520*10^-1 0.521*10^-1
4 00000.000000123 0.0000001230 YES 0.1230*10^-6
4 00100.00000012 100.00000013 YES 0.1000*10^3
5 0010.013 10.012 NO 0.10013*10^2 0.10012*10^2
4 123.5678 123.5 YES 0.1235*10^3
3 123.5678 123 YES 0.123*10^3
4 123.0678 123 YES 0.1230*10^3
3 0.000 0 YES 0.000*10^0
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,exa=0,exb=0,posa=-1,posb=-1;
string a,b;
scanf("%d ",&n);
cin>>a>>b;
posa=a.find('.');posb=b.find('.');
if(posa!=-1)
{
while(a.length()>0&&a[0]=='0'&&a[1]!='.') a.erase(a.begin());
posa=a.find('.');
if(posa==1)
{
if(a[0]=='0')
{
int temp=posa+1;
while(temp<a.length()&&a[temp]=='0')
{
exa--;
temp++;
}
a.erase(a.begin()+posa);
while(a.length()>0&&a[0]=='0') a.erase(a.begin());
if(a.length()==0) a="0";
}
else
{
exa++;
a.erase(a.begin()+posa);
}
}
else
{
exa+=posa;
a.erase(a.begin()+posa);
}
}
else
{
while(a.length()>0&&a[0]=='0') a.erase(a.begin());
exa+=a.length();
if(a.length()==0) a="0";
}
if(posb!=-1)
{
while(b.length()>0&&b[0]=='0'&&b[1]!='.') b.erase(b.begin());
posb=b.find('.');
if(posb==1)
{
if(b[0]=='0')
{
int temp=posb+1;
while(temp<b.length()&&b[temp]=='0')
{
exb--;
temp++;
}
b.erase(b.begin()+posb);
while(b.length()>0&&b[0]=='0') b.erase(b.begin());
if(b.length()==0) b="0";
}
else
{
exb++;
b.erase(b.begin()+posb);
}
}
else
{
exb+=posb;
b.erase(b.begin()+posb);
}
}
else
{
while(b.length()>0&&b[0]=='0') b.erase(b.begin());
exb+=b.length();
if(b.length()==0) b="0";
}
if(a=="0") exa=0;
if(b=="0") exb=0;
string s1=a.substr(0,n);
string s2=b.substr(0,n);
int len1=s1.length(),len2=s2.length();
int len=max(max(len1,len2),n);
while(s1.length()<len) s1+="0";
while(s2.length()<len) s2+="0";
if(exa==exb&&s1==s2) printf("YES 0.%s*10^%d",s1.c_str(),exa);
else printf("NO 0.%s*10^%d 0.%s*10^%d",s1.c_str(),exa,s2.c_str(),exb);
return 0;
}