您的位置:首页 > 其它

HDU1002 A + B Problem II

2017-05-03 09:10 281 查看
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

Recommend

We have carefully selected several similar problems for you: 1004 1005 1009 1020 1010

因为一年多没刷题,所以这算是康复训练。。。

题意: 两个大整数相加

思路:就是按照数学方法,从个位数开始相加,如果满十就进一位,模拟小时候玩的用算盘做加减。

其中CE了一次,发现string的操作要用string头文件啊,和cstring不是一个头文件的说。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int main()
{
string a,b;
int na[8000],nb[8000],sum[8000],pre;
int num;
cin >> num;
for(int j = 1;j<=num;j++)
{
memset(sum,0,sizeof(sum));
memset(na,0,sizeof(na));
memset(nb,0,sizeof(nb));
cin >> a >> b;
pre = 0;
int lena = a.length();
int lenb = b.length();
for(int i = 0;i<lena;i++)
na[lena-i-1] = a[i]-'0';
for(int i = 0;i<lenb;i++)
nb[lenb-i-1] = b[i]-'0';
int lenx = lena > lenb? lena:lenb;
for(int i = 0;i < lenx ;i++)
{
sum[i] = na[i] + nb[i] + pre;
pre = 0;
if(sum[i]>= 10)
{
pre = 1;
sum[i] = sum[i]%10;
}

}
cout << "Case " << j << ":"<<endl;
cout << a << " + " << b << " = ";
for(int i = lenx-1;i>=0;i--)
cout<<sum[i];
cout<<endl;
if(j!=num)cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: