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>
#include<stdio.h>
#include<string.h>
#define max(x,y) ( x>y?x:y )
#define n 1010
using namespace std;
int main()
{
char a1[n]={0};
char b1[n]={0};
int a2[n]={0}, b2[n]={0};
int num ,n1,n2,j,xin,k,i,m1,m2;
cin>>num;
for(i = 1; i <=num; i++){
scanf("%s %s",a1,b1);
n1 = strlen(a1);
n2 = strlen(b1);
for(j = 0 ; j < n1; j++){
if(a1[j] != '0'&& a1[j] != NULL){
m1 = j;
break;
}else if(a1[j] == '0' && j == n1-1){
m1 = 0;
n1 = 1;
break;
}
}
for(j = 0 ; j < n2; j++){
if(b1[j] != '0' && b1[j] != NULL){
m2 = j;
break;
}else if(b1[j] == '0' && j == n2-1){
m2 = 0;
n2 = 1;
break;
}
}
for(j = n1-1,k = 0;j >=m1 ;j--,k++){
a2[k] = a1[j] - '0';
}
for(j = n2-1,k = 0;j >=m2 ;j--,k++){
b2[k] = b1[j] - '0';
}
xin = max(n1-m1,n2-m2);
int c2[n] = {0};
for(j = 0; j < xin ; j++){
c2[j] = a2[j] + b2[j] + c2[j];
c2[j+1] = c2[j] / 10;
c2[j] = c2[j] % 10;
}
if(c2[xin] == 0) xin--;
printf("Case %d:\n",i);
for(j = m1; j<n1;j++){
printf("%c",a1[j]);
}
printf(" + ");
for(j = m2; j<n2;j++){
printf("%c",b1[j]);
}
printf(" = ");
for(j = xin; j>= 0; j--) printf("%d",c2[j]);
if(i != num) {
printf("\n\n");
}else{
printf("\n");
}
for(j = 0; j<=xin; j++){
a2[j] = b2[j] = 0;
}
}
return 0;
}
题解:字符串形式读入两个加数,去掉没用的0,然后按位相加。