//4_2_1: Adding Reversed Numbers反向数相加POJ1504 ZOJ2001
//程序可以运行大型整数,就是比longlong还要long的,因为是用数组处理的
//注意前0和后0都要处理
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
int i,j,N,counta,countb,max,temp,plus;
int a[100],b[100];
char enter,str[100];
scanf("%d%c",&N,&enter);
while(N--)
{
counta = 0;
countb = 0;
plus = 0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin.getline(str,100);
i = 0;
while(str[i] != ' ')
a[counta++] = str[i++] - '0';
i++;
while(str[i])
b[countb++] = str[i++] - '0';
max = counta > countb ? counta : countb;
for(i = 0;i < max;i ++)
{
temp = plus;
plus = (a[i] + b[i] + temp) / 10;
a[i] = (a[i] + b[i] + temp) % 10;
}
if(plus == 1)
{
a[i] = 1;
max++;
}
i = 0;
while(a[i] == 0)
i++;
j = max - 1;
while(a[j] == 0)
j--;
for(;i <= j;i ++)
printf("%d",a[i]);
printf("\n");
}
return 0;
}
/*测试结果:通过POJ1504 ZOJ2001检测
3
24 1
34
4358 754
1998
305 794
1
请按任意键继续. . .
*/
POJ1504 ZOJ2001 Adding Reversed Numbers
最新推荐文章于 2017-12-30 07:59:10 发布