hdu 1002 A + B Problem II 大数相加

做了测试数据又通过了,同样悲剧的是wrong answer, 仔细一看题,数组开小了,然后又提交,继续我的悲剧,Presentation Error,又看看题,原来是最后结束时不用再输出"\n"

 改了过来后。Accepted....希望大家以后注意审题。。。

 

#include<stdio.h>
#include
<string.h>
#include
<math.h>
#include
<stdlib.h>
#define M 1005

int main ( )
{
int a[M],b[M],c[M],i,j,k,len1,len2,flag=0;
char ch1[1005],ch2[1005];
int N,l=1;
scanf(
"%d",&N);
while(N--)
{
int p=0,q=0;
flag
=0;
scanf(
"%s%s",ch1,ch2);
memset(a,
0,sizeof(a));
memset(b,
0,sizeof(b));
memset(c,
0,sizeof(c));
len1
=strlen(ch1);
len2
=strlen(ch2);
for(i=len1-1;i>=0;i--)
a[p
++]=ch1[i]-'0';
for(i=len2-1;i>=0;i--)
b[q
++]=ch2[i]-'0';
k
=len1>len2?len1:len2;
for(i=0;i<k;i++)
c[i]
=a[i]+b[i];
for(i=0;i<k;i++)
{
if(c[i]>9)
{
c[i]
-=10;
c[i
+1]++;
}

}
if(c[k]!=0)
flag
=1;

printf(
"Case %d:\n",l++);
printf(
"%s + %s = ",ch1,ch2);
if(flag)
{
for(i=k;i>=0;i--)
printf(
"%d",c[i]);
printf(
"\n");
}
else
{
for(i=k-1;i>=0;i--)
printf(
"%d",c[i]);
printf(
"\n");
}

if(N)
printf(
"\n");

}
//system(
"pause");
return 0;
}

  代码写的不好,还可以改改。。呵呵。以后有时间我再去优化。。

posted on 2011-03-30 02:44  more think, more gains  阅读(240)  评论(0编辑  收藏  举报

导航