poj_1503 大整数加法

本文深入探讨了代码实现中的两个关键细节:确保保存最终结果的数组足够大以避免越界错误,以及处理可能存在的非零开头数据。通过实例演示如何正确地初始化数组并进行数据输入、处理和输出,确保程序的稳定性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个细节要注意:

(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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值