PAT (Basic Level) 1016 部分A+B

题意

对于给定的数字A和B,各自提取只包含指定字符DA和DB的子序列,对两个子序列求和输出。

思路

模拟即可。注意范围,需要int64_t

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	string a, b;
	int da, db;
	cin >> a >> da >> b >> db;
	auto solve = [](string s, int d) {
		int64_t res = 0;
		for (int i = 0; i < s.size(); ++i) {
			s[i] -= '0';
			if (s[i] != d) continue;
			res = res * 10 + s[i];
		}
		return res;
	};
	cout << solve(a, da) + solve(b, db) << '\n';
	return 0;
} 

HINT

不定时更新更多题解,Basic Level 全部AC代码,详见 link ! ! !

发布了31 篇原创文章 · 获赞 15 · 访问量 765

猜你喜欢

转载自blog.csdn.net/abcdefbrhdb/article/details/104563980