A-A+

A+B Problem II

2012年08月05日 acm未分类 暂无评论 阅读 373 次

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.
样例输入
2
1 2
112233445566778899 998877665544332211
样例输出
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

解题思路:说白了,大数问题,累加,满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 "<

标签:

给我留言

Copyright © C/C++程序员之家 保留所有权利.   Theme  Ality 浙ICP备15011757号-3

用户登录