您的位置:首页 > 其它

poj_1503 大整数加法

2015-01-27 22:18 204 查看
两个细节要注意:

(1)、保存最终结果的数组要开102的大小!

(2)、存在001这样的数据!所以开始的判断要:if (!strcmp(s,"0"))

code:

<span style="font-size:18px;">#include <iostream>
#include <fstream>

using namespace std;

char s[101];
int ans[102];

int main()
{
	fstream in("input.txt");
	while (cin >> s)
	{
		if (!strcmp(s,"0"))
			break;
		int len = strlen(s);
		for (int i = 0; i<len; i++){
			ans[i] += s[len - i - 1] - '0';
		}
		for (int i = 0; i <= 101; i++){ 
			if (ans[i] >= 10){
				ans[i + 1] += ans[i] / 10;
				ans[i] %= 10;
			}
		}
	}
	int end = 101;
	while (ans[end] == 0)
		end--;
	for (int j = end; j >= 0; j--)
		cout << ans[j];
	cout << endl;
	//system("pause");
	return 0;
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: