题意分析
将两个反向数进行求和,之后再反向,输出是去掉前导零。
代码总览
#include<cstdio>
#include<cstring>
#include<algorithm>
#define nmax 200
using namespace std;
char num1[nmax],num2[nmax],sum[nmax];
int len1,len2;
int Read(char num[]){
scanf("%s",num);
int len = strlen(num);
int tag = len -1;
while(num[tag] == '0' && tag <= len-1 && tag >=0){
num[tag] = '\0';
tag --;
}
len = tag+1;
for(int i = 0;i<len;++i) num[i]-= '0';
return len;
}
void Plus(){
int len = max(len1,len2);
int tot = len;
for(int i = 0;i<len;++i) sum[i] = num1[i] + num2[i];
for(int i = 0;i<len;++i) {
sum[i+1] += sum[i] / 10;
sum[i] %= 10;
}
if(sum[len] !=0) tot++;
bool flag = false;
for(int i = 0;i<tot;++i) {
if(sum[i] == 0 && !flag) continue;
else flag = true;
if(flag) printf("%d",sum[i]);
}
printf("\n");
}
int main(){
// freopen("in.txt","r", stdin);
int t;
scanf("%d",&t);
while(t--){
memset(sum,0,sizeof sum);
memset(num1,0,sizeof num1);
memset(num2,0,sizeof num2);
len1 = Read(num1);
len2 = Read(num2);
Plus();
}
return 0;
}