Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2 1 2 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
本题是大数相加的题,最容易的就是把字符串倒着赋值给整型字符串,然后按照从前到后的循序相加,然后倒着输出
因为结果只可能比最长的那个字符串多一位,所以只需要取它的长度作为结果就好
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,m,n,l,k,s,c[10010],d[10010],e[10010],f=1;
char a[10010],b[10010];
scanf("%d",&n);
while(n--)
{
if(f!=1)
printf("\n");
scanf("%s %s",a,b);
l=strlen(a);
k=strlen(b);
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
for(i=l-1,j=0; i>=0; i--)
c[j++]=a[i]-'0';
for(i=k-1,j=0; i>=0; i--)
d[j++]=b[i]-'0';
s=l>k?l:k;
for(i=0; i<s; i++)
{
if(c[i]+d[i]>9)
{
c[i]+=d[i]-10;
c[i+1]+=1;
}
else
c[i]+=d[i];
}
printf("Case %d:\n%s + %s = ",f++,a,b);
if(c[s])
printf("%d",c[s]);
for(i=s-1; i>=0; i--)
printf("%d",c[i]);
printf("\n");
}
return 0;
}