POJ 1504
题意
把两个数字翻转之后相加,再翻转回来的到结果
思路
翻转之后再翻转回来,所以可以视为不用翻转,从左向右加,进位向右进位。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int n1,n2;
int a[100],b[100];
void add(){
for(int i=0;i<(n1>n2?n1:n2);i++){
a[i] = a[i] + b[i];
if(a[i]>9){
a[i] -= 10;
a[i+1]++;
}
}
}
int main(){
int t;
char s1[100],s2[100];
scanf("%d",&t);
while(t--){
scanf("%s%s",s1,s2);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
n1 = strlen(s1);
n2 = strlen(s2);
for(int i = 0;i<n1;i++){
a[i] = s1[i] - '0';
}
for(int i =0;i<n2;i++){
b[i] = s2[i]-'0';
}
add();
int right = 0;
while(1){
if(a[right] == 0)
right++;
else
break;
}
int left = n1 + n2;
while(1){
if(a[left]==0)
left--;
else
break;
}
for(int i = right;i<=left;i++){
printf("%d",a[i]);
}
printf("\n");
}
}