1060 Are They Equal (25 point(s))

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

经验总结:

这一题,难度还可以,就是细节的处理让人摸不着头脑,题目也没有任何提示,而且题目的测试集还不严谨.....很想跟他们反馈却根本找不到反馈的地方(遇到不严谨的地方不止一次了)
首先,说说这一题的不严谨的地方吧,对于最低限度不严谨的AC的代码,如果你输入  3 123 0.123   ,竟然可以得出   这两个浮点数是相等的结论.....  很明显,是没有判断指数是否相等。
然后,就是要注意这样一个样例 3 000002.0000001 2 这两个浮点数是相等的,就是注意前导0的处理。
最后,要注意0的处理,比如输出样例 3 0 000000.0000000,应该相等,输出的时候还要按照指定形式进行输出,输出应该为,YES 0.000*10*0
就这么多啦~

AC代码

#include <cstdio>
#include <cstring>
const int maxn=110;

int find(char str[])
{
int i;
for(i=0;str[i]!='\0';++i)
if(str[i]=='.')
return i;
return i;
}
int dispose(char a[],int n,char c[],int &pos)
{
int num=0;
int len=strlen(a);
int xpos=0;
while(a[xpos]=='0'&&xpos<pos)
++xpos;
for(int i=xpos;i<pos&&num<n;++i)
c[num++]=a[i];
if(n-num>0)
{
if(num==0)
{
xpos=pos+1;
while(a[xpos]=='0')
++xpos;
for(int i=xpos;i<len&&num<n;++i)
c[num++]=a[i];
}
else
for(int i=pos+1;i<len&&num<n;++i)
c[num++]=a[i];
}
if(num==0)
{
pos=0;
xpos=0;
}
int x=n-num;
for(int i=0;i<x;++i)
c[num++]='0';
c[num]='\0';
return xpos;
}
int main()
{
int n,apos0,bpos0;
char a[maxn],b[maxn],coma[maxn],comb[maxn];
scanf("%d %s %s",&n,a,b);
int posa=find(a);
int posb=find(b);
apos0=dispose(a,n,coma,posa);
bpos0=dispose(b,n,comb,posb);
if(posa-apos0<0)
++posa;
if(posb-bpos0<0)
++posb;
if(strcmp(coma,comb)==0&&posa-apos0==posb-bpos0)
printf("YES 0.%s*10^%d\n",coma,posa-apos0);
else
printf("NO 0.%s*10^%d 0.%s*10^%d\n",coma,posa-apos0,comb,posb-bpos0);
return 0;
}