📄 hdoj1002.txt
字号:
A + B Problem II
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4941 Accepted Submission(s) : 855
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
/*
农夫三拳@seu
drizzlecrj.gmail.com
2007-03-02
*/
#include <iostream>
#include <string>
using namespace std;
void Add(const string& n1, const string& n2)
{
int len1 = n1.length(), len2 = n2.length();
int i = len1 - 1, j = len2 - 1;
int result[1001] = {0};
int carry = 0;
int index = 0;
while(i >= 0 || j >= 0 || carry > 0)
{
if(i >= 0)
result[index] += n1[i] - '0';
if(j >= 0)
result[index] += n2[j] - '0';
result[index] += carry;
carry = result[index] / 10;
result[index] -= carry * 10;
++index, --i, --j;
}
for(i = index - 1; i >= 0; i--)
cout << result[i];
cout << endl;
}
int main()
{
int t;
string n1, n2;
cin >> t;
int c = 1;
while(t--)
{
cin >> n1 >> n2;
if(c != 1)
cout << endl;
cout << "Case " << c++ << ":" << endl;
cout << n1 << " + " << n2 << " = ";
Add(n1, n2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -