424 Integer Inquiry

水题,就是把非常大的数组进行相加

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
char table_atoi[128];//下标字母 对应  数字
int main()
{
	int i =0;
	char c = '0';
	for( ; i < 10 ; ++i ,++c)
	{
		table_atoi[c] = i;

	}
	char buff[100];
	char result[100] = {0};
	char carry = 0;
	int result_len = 0;
	int temp;
	while(cin>>buff)
	{
		if(buff[0] == '0') break;
		int len = strlen(buff)-1;//从个位开始
		i= 0;
		carry = 0;
		while(len >= 0)
		{
			temp = table_atoi[buff[len]]+result[i]+carry;
			carry = (temp>=10?1:0);
			temp = (carry ==0 ? temp:temp-10);
			result[i] = temp;
			i++;
			len --;
		}
		if(carry != 0)
		{
			while(carry != 0)
			{
				temp = result[i]+carry;
				carry = (temp>=10?1:0);
				temp = (carry ==0 ? temp:temp-10);
				result[i++] = temp;
			}
		}
		if(i > result_len) result_len = i;
	}

	result[result_len] = 0;
	reverse(result,result+result_len);
	transform(result,result+result_len,result,bind2nd(plus<int>(),48));
	cout<<result<<endl;
	cin>>i;
	return 0;
}

 

posted @ 2012-06-25 22:51  godjob  Views(194)  Comments(0Edit  收藏  举报