A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 314078 Accepted Submission(s): 60863
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
Author
Ignatius.L
最基础的大数题,部分细节详见代码。总的来说就是用数组模拟10进制运算,注意位数问题。
#include<stdio.h>
#include<string.h>
char a[1111],b[1111],c[1111],d[1111],sum[1111];
int main()
{
int T,i,j;
scanf("%d",&T);
int n=T;
int k=1;
while(T--)
{
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
memset(sum,0,sizeof(sum));
scanf("%s%s",a,b);
int l1=strlen(a);
int l2=strlen(b);
for(i=l1-1,j=0;j<l1;j++,i--)
c[j]=a[i];
for(i=l2-1,j=0;j<l2;j++,i--)
d[j]=b[i];
int l=l1;
if(l2>l)
l=l2;
int p=0,q=0;
for(i=0;i<l;i++)
{
if(c[i]-'0'>=0)
p+=c[i]-'0';
if(d[i]-'0'>=0)
p+=d[i]-'0';
p+=q;
q=p/10;
p=p%10;
sum[i]=p+'0';
p=0;
}
if(q!=0)
sum[i++]=q+'0';
printf("Case %d:\n",k++);
printf("%s + %s = ",a,b);
for(j=i-1;j>=0;j--)
printf("%c",sum[j]);
printf("\n");
if(k!=n+1)
printf("\n");
}
return 0;
}