POJ1503 ZOJ1292 Integer Inquiry

//4_7_23:	Integer Inquiry 高精度整数累加 POJ1503 ZOJ1292
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
	int i,j,temp,plus,count,length;
	char str[110][110];
	int ans[210];
	int cnt = 0;
	int len = 0;
	while(1)
	{
		cin.getline(str[cnt],110);
		length = strlen(str[cnt]);
		if(length == 1 && str[cnt][0] == '0')	break;
		i = 0;
		while(str[cnt][i] == '0')	i++;
		if(i > 0)	for(j = i;j <= length;j ++)	str[cnt][j - i] = str[cnt][j];
		length = strlen(str[cnt]);
		if(length > len)	len = length;
		for(i = 0,j = length - 1;i < j;i ++,j --)
		{
			temp		= str[cnt][i];
			str[cnt][i] = str[cnt][j];
			str[cnt][j] = temp;
		}
		cnt++;
	}
	for(i = 0;i < cnt;i ++)
	{
		length = strlen(str[i]);
		if(length < len)
		{
			for(j = length;j < len;j ++)	str[i][j] = '0';
			str[i][len] = '\0';
		}
	}
	memset(ans,0,sizeof(ans));
	plus = 0;
	for(j = 0;j < len;j ++)
	{
		for(i = 0;i < cnt;i ++)	ans[j] += (int)(str[i][j] - '0');
		temp   = plus;
		plus   = (ans[j] + temp) / 10;
		ans[j] = (ans[j] + temp) % 10;
	}
	count = len;
	while(plus != 0)
	{
		ans[count++] = plus % 10;
		plus = plus / 10;
	}
	for(i = count - 1;i >= 0;i --)	printf("%d",ans[i]);
	printf("\n");
	return 0;
}
/*测试结果:通过POJ1503检测,ZOJ1292是不同格式的输入和输入,需要修改
123134
23498743293207
00002344398723232
9234
00234
0
25843142149041
请按任意键继续. . .
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值