hdu1002 A + B Problem II 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
字符串处理(大数A+B)
题目大意:计算A+B,格式输出。
题目分析:用字符串模拟进位计算,注意细微的格式要求,水过。
以下是代码:
#include<stdio.h>
#include<string.h>
int main()
{
char c1[1005],c2[1005];
int t,c,i,j,a[1005],l;
while(scanf("%d",&t)!=EOF)
{
for(i=1;i<=t;i++)
{
memset(a,0,sizeof(a));
memset(c1,48,1005);
memset(c2,48,1005);
scanf("%s%s",c1,c2);
printf("Case %d:\n%s + %s = ",i,c1,c2);
strrev(c1);//倒转字符串以实现从低位向高位加
strrev(c2);
l=strlen(c1)>strlen(c2)?strlen(c1):strlen(c2);
c1[strlen(c1)]=c2[strlen(c2)]=48;
c=0;
for(j=0;j<l;j++)
{
a[j]=c1[j]-'0'+c2[j]-'0'+c;
//printf("%c : %d ;%c : %d .\n",c1[j],c1[j]-'0',c2[j],c2[j]-'0');
c=a[j]/10;
a[j]%=10;
}
if(c)printf("%d",c);
for(j=l-1;j>=0;j--)
{
printf("%d",a[j]);
}
printf("\n");
if(t-i)printf("\n");
}
}
return 0;
}
PS:被格式坑惨了,注意空一行是在两case间,最后一case输出完结果后只有一个换行。