A+B Problem II

A+B Problem II

A+B Problem II

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

A,B must be positive.

输入
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.
样例输入
样例输出

解题思路:说白了,大数问题,累加,满10进位,保存进位到变量,相加继续处理!一个字符串处理结束,考虑另外一个字符串!
给出几组特殊的测试数据999 1;1 11;00 09;1 9;0 0;
注意该题不用考虑最高位为0,例如09即可!
[cpp]
#include
#include
using namespace std;
int main()
{
string s1,s2;int a[1005];
int i,j,k,t,d,x=1;cin>>d;
while(d–)
{
cin>>s1>>s2;
i=s1.size()-1;j=s2.size()-1;t=k=0;
while(i>=0&&j>=0)
{
a[k]=(s1[i]+s2[j]+t-96)%10;
if(s1[i]+s2[j]+t-96>=10) t=1;
else t=0;
k++;i–;j–;
}
if(i>=0)
{
while(i>=0)
{
a[k]=(s1[i]+t-48)%10;
if(s1[i]+t-48>=10) t=1;
else t=0;
k++;i–;
}
}
if(j>=0)
{
while(j>=0)
{
a[k]=(s2[j]+t-48)%10;
if(s2[j]+t-48>=10) t=1;
else t=0;
k++;j–;
}
}
if(t) a[k++]=t;//处理最高位
cout<<"Case "<