链接:https://ac.nowcoder.com/acm/problem/15902
来源:牛客网
There are many difficulties in today's contest, but this is definitely not one of them.
Because I want you to do it, the classic A+B.
输入描述:
First line an Integer N means n test cases. Next 2N lines representing 2N groups of different Integers. N<50. THE LENGTH of every Integer will less than 5000. All Integers will be greater than 0. Some cases may have leading zero.
输出描述:
One line for each test cases reprecenting the answer of A+B. Do not putout leading zero
示例1
输入
1 100000000000000 100000000000000
输出
200000000000000
备注:
You do not have to use FFT.
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
struct bign{
int d[6000];
int len;
bign(){//初始化
memset(d,0,sizeof(d));
len=0;
}
};
bign change(char str[]){
bign a;
a.len=strlen(str);
for(int i=0;i<a.len;i++){
a.d[i]=str[a.len-1-i]-'0';
}
return a;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
char str1[6000],str2[6000];
scanf("%s%s",str1,str2);
bign a=change(str1);
bign b=change(str2);
bign c;//计算求和
int carry=0;
for(int i=0;i<a.len||i<b.len;i++){
int temp=a.d[i]+b.d[i]+carry;
c.d[c.len++]=temp%10;
carry=temp/10;
}
if(carry!=0){//进位最后不为1
c.d[c.len++]=carry;
}
int j=c.len;
for(j;j>=0;j--){//忽略掉前导0
if(c.d[j]!=0){
break;
}
}
for(int i=j;i>=0;i--){
printf("%d",c.d[i]);
}
printf("\n");
}
return 0;
}