A + B Problem II HDU - 1002 高精度
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
InputThe 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.
OutputFor 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
#include<iostream> using namespace std; int main() { int T, n = 0; cin >> T; while (T--) { char a[1005], b[1005];int c[1005]; cin >> a >> b; int A, B, C, D; A = strlen(a); B = strlen(b); int temp1 = 0; int i=A, j=B, k=0; while(1) { C = (a[i-1] - '0'); D =( b[j-1] - '0');//转换成int型 if (i <= 0)C =0; if (j <= 0)D =0; c[k] = ( temp1 + C + D )% 10;//记录 temp1 =( temp1 + C + D )/ 10;//进位 i--;j--; if (i <= 0 && j <= 0)break; k++; } c[k + 1] = '\0'; cout << "Case " << ++n << ":" << endl;//注意输出格式 cout << a << " + " << b << " = "; for (int i = k; i>=0; i--)//将c倒序输出 cout <<c[i]; cout<< endl; if (T != 0)cout <<endl;//两个输出之间有一个空行 } return 0; }