I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
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.
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.
2
1 2
112233445566778899 998877665544332211
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
下面是我的代码:
#include<iostream>
#include<string>
using namespace std;
void main()
{
char A[1000],B[1000];
int sum[1001];
int n,i;
while(cin>>n)
{
int index(0),sumindex(0);
for(i=1;i<=n;i++)
{
memset(sum,0,sizeof(sum));
cin>>A>>B;
int index(0),sumindex(0);
for(index=strlen(A)-1,sumindex=1000;index>=0;index--,sumindex--)
{
sum[sumindex]+=A[index]-'0';
}
for(index=strlen(B)-1,sumindex=1000;index>=0;index--,sumindex--)
{
sum[sumindex]+=B[index]-'0';
}
for(sumindex=1000;sumindex!=0;sumindex--)
{
if(sum[sumindex]>9)
{
sum[sumindex-1]+=sum[sumindex]/10;
sum[sumindex]=sum[sumindex]%10;
}
}
for(sumindex=0;sumindex<=1000;sumindex++)
{
if(sum[sumindex]!=0)
{
break;
}
}
if(sumindex=1001)
{
sumindex=1000;
}
cout<<"Case "<<i<<":"<<endl;
cout<<A<<" + "<<B<<" = ";
sumindex=0;
while(!sum[sumindex])sumindex++;
for(;sumindex<=1000;sumindex++)
{
cout<<sum[sumindex];
}
cout<<endl;
if(i-n){cout<<endl;cout<<endl;}
}
break;
}
}
有条件的可以将我的代码放到你的编译器上运行一下,注意观察我的运算结果和题目要求,但是我提交的时候显示:Presentation Error