Situation analysis
- The first case: the two digits are the same
- The two digits are the same and the digits remain the same after addition, for example: 22+33=55;
- Two digits are the same and add 1 to the digit after adding, for example: 999+999=1998;
- The second case: the two digits are different
- The result digit is equal to max (the digit of the larger digit), for example: 123+1=124;
- The number of result bits is equal to max+1, for example, 999+1=1000;
#include <stdio.h>
#include <string.h>
int CharToInt(char ch){
return (ch-'0');
}
int main(){
char a[1000],b[1000];
int num[1001];
int n,i,j=1,al,bl;
int k,t;
scanf("%d",&n);
while (n--) {
if(j!=1)
printf("\n");
scanf("%s",a);
al=(int)strlen(a);
scanf("%s",b);
bl=(int)strlen(b);
k=al>bl?al:bl;
t=k;
for(i=0;i<=k;i++)
num[i]=0;
for(;al>0&&bl>0;k--){
num[k]+=CharToInt(a[--al])+CharToInt(b[--bl]);
if(num[k]/10){
num[k-1]++;
num[k]%=10;
}
}
while (al>0) {
num[k--]+=CharToInt(a[--al]);
if(num[k+1]/10){
num[k]++;
num[k+1]%=10;
}
}
while (bl>0) {
num[k--]+=CharToInt(b[--bl]);
if(num[k+1]/10){
num[k]++;
num[k+1]%=10;
}
}
printf("Case %d:\n",j++);
printf("%s + %s = ",a,b);
for(i=0;i<=t;i++)
{
if(i==0&&num[i]==0)
i++;
printf("%d",num[i]);
}
printf("\n");
}
return 0;
}