PAT 乙级-1074(测试点1,3,5)

测试点1,3:是因为最后得到的PAT数可能会超出N位, 如:

30527
26203
20415           //   输出为 117201

测试点5:是因为可能得到的PAT数为0,此时需输出0

正确代码

#include<iostream>
using namespace std;

int main() {
	string table, num1, num2, res;
	int temp = 0, c = 0, len;
	cin >> table >> num1 >> num2;
	len = table.length();
	if(num1.length() < len) num1.insert(0, len - num1.length(), '0');
	if(num2.length() < len) num2.insert(0, len - num2.length(), '0');
	for(int i = len - 1; i >= 0; i--) {
		int temp = table[i] -'0' == 0 ? 10 : table[i] -'0', sum = 0; 
		if(num1[i] != '0' || num2[i] != '0'){
            res = res + to_string((num1[i] - '0' + num2[i] - '0' + c) % temp);
        } else res = res + to_string(c % temp);
		c = (num1[i] - '0' + num2[i] - '0' + c) / temp;
	}
	if(c) res = res + to_string(c);
	temp = 0;
	for(int i = res.length() - 1; i >= 0; i--) {
		if(res[i] != '0') temp = 1;
		if(temp) cout << res[i];
	}
    if(temp == 0) cout << 0;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值