POJ1504 ZOJ2001 Adding Reversed Numbers

//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
请按任意键继续. . .
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值