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
注意點:
1:注意進位
2:注意兩數相加時加完後長度要湊足,不然會缺了長度較長的那個數的最高的幾位。
代碼:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<list>
#include<iterator>
#include<stack>
#include <queue>
#include <cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define e 2.718281828459
#define INF 0x7fffffff
#pragma warning(disable:4996)
#define sf scanf
#define pf printf
#define sf2d(x,y) scanf("%d %d",&(x),&(y))
#define sfd(x) scanf("%d",&x)
#define sff(p) scanf("%lf",&p)
#define pfd(x) printf("%d\n",x)
#define mset(x,b) memset((x),b,sizeof(x))
const double pi = acos(-);
int main(void) {
int a[];
int b[];
int c[];
int n;
sfd(n);
int Case = ;
while (n--) {
int i=, j=;
char temp[];
scanf("%s", temp);
int len = strlen(temp);
for (int p = ; p < len; p++) {
a[i++] = temp[p] - '0';
}
scanf("%s", temp);
len = strlen(temp);
for (int p = ; p < len; p++) {
b[j++] = temp[p] - '0';
}
j--, i--;
mset(c, );
int k = i > j ? i : j;
int lena = i;
int lenb = j;
len = k;
while (i&&j) {
c[k] += a[i] + b[j];
if (c[k] >= ) {
c[k - ] += c[k] / ;
c[k] %= ;
}
k--;
j--;
i--;
}
while(i) {
c[k] += a[i];
if (c[k] >= ) {
c[k - ] += c[k] / ;
}
k--;
i--;
}
while (j) {
c[k] += b[j];
if (c[k] >= ) {
c[k - ] += c[k] / ;
}
k--;
j--;
}
printf("Case %d:\n", Case++);
for (int i = ; i <= lena; i++)
printf("%d", a[i]);
printf(" + ");
for (int i = ; i <= lenb; i++)
printf("%d", b[i]);
printf(" = ");
for (int i = ; i <= len; i++) {
if (c[]&&i==)
printf("%d", c[]);
printf("%d", c[i]);
}
printf("\n");
if (n != )
printf("\n");
}
return ;
}