POJ 1503 Integer Inquiry 高精度加法

题意:输入若干个大整数,求其和。

#include <cstring>
#include <iostream>
using namespace std;

int sum[111];
char str[101];

int main()
{
	int i, j, len;
	while ( cin >> str )
	{
		len = strlen(str);
		if ( strcmp(str,"0") == 0 ) break;
		for ( i = 110, j = len - 1; j >= 0 && i >= 0; i--, j-- )
			sum[i] += str[j] - '0';
	}
	
	for ( i = 110; i > 0; i-- )
	{
		sum[i-1] += sum[i] / 10;
		sum[i] %= 10;
	}

	for ( i = 0; sum[i] == 0 && i < 111; i++ );
	if ( i == 111 ) cout << 0;
	for ( ;i < 111; i++ ) cout << sum[i];
	cout << endl;
	return 0;
}


你可能感兴趣的:(Integer)