I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
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. Output a blank line between two test cases.
大整数加法
#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了好几遍。。。