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
大整數加法
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 1009;
char a[MAX], b[MAX];
int num1[MAX], num2[MAX], ans[MAX];
int len_a, len_b;
void big_add()
{
int carry = 0;
int len = len_a > len_b ? len_a : len_b;
for(int i = 0; i < len; i++)
{
int sum = num1[i] + num2[i] + carry;
ans[i] = sum % 10;
carry = sum / 10;
}
if(carry)
{
ans[len] = carry;
len ++;
}
for(int i = len - 1; i >= 0; i--)
printf("%d", ans[i]);
printf("\n");
}
int main()
{
int num, case_n;
case_n = 1;
scanf("%d", &num);
while(num--)
{
memset(num1, 0, sizeof(num1));
memset(num2, 0, sizeof(num2));
scanf(" %s %s", a, b);
len_a = strlen(a);
len_b = strlen(b);
int j = 0;
for(int i = len_a - 1; i >= 0; i--)
num1[j++] = a[i] - '0';
j = 0;
for(int i = len_b - 1; i >= 0; i--)
num2[j++] = b[i] - '0';
printf("Case %d:\n", case_n ++);
printf("%s + %s = ", a, b);
big_add();
if(num)
printf("\n");
}
return 0;
}
話說太久不刷題是不行啊,一個普通的大整數加法竟然都WA了好幾遍。。。