POJ1503大整数相加

在字符串里存入输入的大数,然后转存到整形数组temp中,一位一位相加,判断是否进位,因为数字的高低位和数组的高低位不同,所以temp中数字顺序要反过来,然后输出result时,倒序输出就是结果。

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
	char a[101] = { '\0' };             
	int temp[110], result[110];     
	for (int i = 0; i < 110; i++)
	{
		temp[i] = 0;
		result[i] = 0;
	}
	while (cin >> a)
	{
		int size_a = strlen(a);
		if (size_a == 1 && a[0] == '0')
			break;
		for (int i = 0; i < size_a; i++)
		{
			temp[i] = a[size_a - i - 1] - '0';
		}
		for (int j = 0; j < 110; j++)
		{
			result[j] += temp[j];
			if (result[j] >= 10)
			{
				result[j] -= 10;
				result[j + 1] += 1;
			}
		}
	}
	int flag = 1;
	int index = 0;
	for (int i = 109; i != 0 && flag == 1; i--)
	{
		if (result[i] != 0)
		{
			index = i;
			flag = 0;
		}
	}
	for (int i = index; i >= 0; i--)
	{
		cout << result[i];
	}
	cout << endl;
	system("pause");
	return 0;
}

运行结果为:
与答案结果一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值