PAT乙级1074 宇宙无敌加法器 (20 分)测试点分析

博客详细解析了PAT乙级编程题1074的难点,重点指出进制表示中‘d’和‘0’代表10进制,以及结果为0和进位处理的特殊情况。内容围绕问题的测试点进行了深入分析。

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

https://pintia.cn/problem-sets/994805260223102976/problems/994805263297527808

这道题做的真是稀碎…
主要坑点:
进制标中“d”和“0”表示10进制
结果为0
结果的最后一个carry(进位)不等于0时

#include <iostream>
#include <algorithm>
using namespace std;
int D[22] = {0}, a[22] = {0}, b[22] = {0};
int f1(int a[], string tmp){
	for(int i = 0; i < tmp.size(); i++){
		a[i] = (tmp[i]=='d' || tmp[i]=='0') ? 10 : tmp[i]-'0';
	}
}

int f2(int a[], string tmp){
	for(int i = 0; i < tmp.size(); i++){
		a[i] = tmp[i]-'0';
	}
}

int main(){
	int N = 0, result[100] = {0};
	string tmp, tmp1, tmp2;
	cin >> tmp >> tmp1 >> tmp2;
	reverse(tmp.begin(), tmp.end()), reverse(tmp1.begin(),tmp1.end()), reverse(tmp2.begin(),tmp2.end());
	f1(D, tmp); f2(a, tmp1); f2(b, tmp2);
	N = tmp.size();
	int carry = 0, i = 0, flag = 0;
	for(i = 0; i < N; i++){
		result[i] = (a[i]+b[i]+carry) % D[i];
		carry = (a[i]+b[i]+carry) / D[i];
	}
	result[N] = carry; 
	for(int j = N; j >= 0; j--){
		if(result[j] || flag){
			flag = 1;
			cout << result[j];
		}	
	}
	if(!flag)	cout << "0";	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值