HDOJ-2054(大数比较(有点坑))(A == B ?)
A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 79376 Accepted Submission(s): 12573
Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
Input
each test case contains two numbers A and B.
Output
for each case, if A is equal to B, you should print "YES", or print "NO".
Sample Input
1 2 2 2 3 3 4 3
Sample Output
NO YES YES NO
后来才想到这些特殊的测试数据,来验证,结果证明前面写的代码通不过这些数据,于是经过2、3个小时的修改最终AC了。
测试数据(附加)
/*
-000000000000000000000000000052.000000000000000
-000000000000000000000000000052
(yes)
-00000000005200000000000.0000000
-000000000052
(no)
0000000000000.000000000000000000000
0
(yes)
0000000000005200000.0000000000
5200000
(yes)
0000000000000000000000000000000000000052000000000000000000000000000000000
000000000052000000000000000000000000000000000
(yes)
000000000000009990009000
00000000000000000000000000000000009990009000
(yes)
*/
-000000000000000000000000000052.000000000000000
-000000000000000000000000000052
(yes)
-00000000005200000000000.0000000
-000000000052
(no)
0000000000000.000000000000000000000
0
(yes)
0000000000005200000.0000000000
5200000
(yes)
0000000000000000000000000000000000000052000000000000000000000000000000000
000000000052000000000000000000000000000000000
(yes)
000000000000009990009000
00000000000000000000000000000000009990009000
(yes)
*/
这些特殊数据实在不好想到,若你也在困扰怎么AC不了,特殊数据,写在这里希望能帮到你。
My solution:
/*2015.11.15*/
#include<stdio.h>
#include<string.h>
char c[100000],n[100000];
int main()
{
char *c1,*p1;
int t1,t11,t22,t2,i,j,c11=0,c12=0,g1,g2;
while(scanf("%s%s",&c,&n)==2)
{
g1=0;g2=0;/*标记两个数字是不是负数,负数和正数的判断过程不同,见上面的补充数据*/
if(c[0]=='-'&&n[0]!='-'||c[0]!='-'&&n[0]=='-')/*判断两个数是不是都负数*/
{
printf("NO\n"); continue;
}
t1=t11=strlen(c);
t2=t22=strlen(n);
if(c[0]=='-'&&n[0]=='-')/*若是负数则从符号位的后面的数字开始比对*/
{
c1=c+1; /*数组地址加1,跳过符号位进行处理.如:-00005与-000000000015,*/
p1=n+1;/*只比较00005与000000000015,经过处理后最终比较5与15,去掉前缀0*/
t1-=1;
t2-=1;
g1=1;
g2=1;
}
else
{
c1=c;
p1=n;
}
c11=0;c12=0;/*标记两个数字,是否是小数,小数和实数的处理不同,如:50000与5.000000,*/
/*50000后面的0是有效数字在比较时不能去掉,5.000000后面的0是无效数字,需去掉*/
for(i=0;i<t1;i++)
if(c[i]=='.')
{
c11=1;
break;
}
for(i=0;i<t2;i++)
if(n[i]=='.')
{
c12=1;
break;
}
for(i=g1;i<t11;i++)
if(c[i]=='0'&&(i<=t11-2&&c[i+1]!='.'))
{
t1--;
c1++;
}
else
break;
for(j=g2;j<t22;j++)
if(n[j]=='0'&&(j<=t22-2&&n[j+1]!='.'))/*若是000000000000,处理后应为:0(保留一个0),而000000000000.5,处理后只剩下0.5(保留小数点前面的一个0)*/
{
p1++;
t2--;/*处理后的数据长度*/
}
else
break;
for(i=t1-1;i>=0;i--)
if(c11)/*去除实数后面的后缀0, 如0.500000,处理后为0.5*/
{
if(c1[i]=='0');
else
{
if(c1[i]=='.')/*0.00000与0比较时,0.00000处理后为0*/
c1[i]='\0';
else
c1[i+1]='\0';/*0.500000,处理后为0.5*/
break;
}
}
for(i=t2-1;i>=0;i--)
if(c12)
{
if(p1[i]=='0');
else
{
if(p1[i]=='.')
{
p1[i]='\0';
}
else
p1[i+1]='\0';
break;
}
}
if(!strcmp(c1,p1))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}