Problem 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
分析
大整數(shù)相加問(wèn)題。另外輸出的格式需要注意
#include <stdio.h>
int main()
{
int n;
int no=0;
char a[1001],b[1001];
scanf("%d",&n);
while(n--)
{
if(no!=0)
printf("\n");
no++;
scanf("%s %s",&a,&b);
int alength=0;
while(a[alength]!='\0')
alength++;
int blength=0;
while(b[blength]!='\0')
blength++;
int length=alength;
if(length>blength)
length=blength;
int ans[1001];
for(int i=0;i<1001;i++)
ans[i]=0;
for(int i=0;i<length;i++)
{
int t1=a[alength-1-i]-'0';
int t2=b[blength-1-i]-'0';
if(t1+t2+ans[i]>9)
{
ans[i]=(t1+t2+ans[i])%10;
ans[i+1]=1;
}
else
{
ans[i]=t1+t2+ans[i];
}
}
if(length<alength)
{
for(int i=length;i<alength;i++)
{
if(a[alength-1-i]-'0'+ans[i]>9)
{
ans[i]=(a[alength-1-i]-'0'+ans[i])%10;
ans[i+1]=1;
}
else
{
ans[i]=a[alength-1-i]-'0'+ans[i];
}
}
}
if(length<blength)
{
for(int i=length;i<blength;i++)
{
if(b[blength-1-i]-'0'+ans[i]>9)
{
ans[i]=(b[blength-1-i]-'0'+ans[i])%10;
ans[i+1]=1;
}
else
{
ans[i]=b[blength-1-i]-'0'+ans[i];
}
}
}
printf("Case %d:\n",no);
printf("%s + %s = ",a,b);
int anslength=1000;
while(ans[anslength]==0)
anslength--;
for(int i=anslength;i>=0;i--)
printf("%d",ans[i]);
printf("\n");
}
}