在做ACM 1002题时,整理得到。
#include<stdio.h>
#include<string.h>
#define MAX
1000
void zero(char *s,int len)
{
int
i;
for(i=0;i<len;i++)
s[i]-=‘0‘;
}
void back(char *s,int
len)
{
int i;
for(i=0;i<len;i++)
s[i]+=‘0‘;
}
int
main()
{
char a[20][MAX],b[20][MAX],c[20][MAX+2];
int
alen,blen;
int
i,T,k[20]={0},t,j,n;
do
{
scanf("%d",&T);
}while(T<=0||T>20)
;
for(i=0;i<T;i++)
{
scanf("%s
%s",a[i],b[i]);
alen=strlen(a[i])
;
blen=strlen(b[i]);
zero(a[i],alen);
zero(b[i],blen);
n=alen>blen?alen:blen;
for(j=0;j<n+5;j++)
c[i][j]=0;
for(j=alen-1,n=blen-1;(n>=0)&&(j>=0);j--,n--,k[i]++)
{
if((c[i][k[i]]=a[i][j]+b[i][n]+c[i][k[i]])
>9)
{
c[i][k[i]+1]=1;
c[i][k[i]]=c[i][k[i]]%10;
}
}
while(j>=0)
{
if((c[i][k[i]]=c[i][k[i]]+a[i][j])>9)
c[i][k[i]+1]=1;
c[i][k[i]]=c[i][k[i]]%10;
j--;
k[i]++;
}
while(n>=0)
{
if((c[i][k[i]]=c[i][k[i]]+b[i][n])>9)
c[i][k[i]+1]=1;
c[i][k[i]]=c[i][k[i]]%10;
n--;
k[i]++;
}
for(;c[i][k[i]]==0;k[i]--);
back(a[i],alen);
back(b[i],blen);
}
for(i=0;i<T;i++)
{
printf("Case %d:\n",i+1); printf("%s + %s =
",a[i],b[i]);
for(;k[i]>=0;k[i]--)
printf("%d",c[i][k[i]]);
if(i<T-1)printf("\n\n");
else
printf("\n");
}
return 0;
}