PAT (Basic Level) 1064 朋友数

题意

如果两个整数各位数字的和是一样的,则被称为是“朋友数”,而那个公共的和就是它们的“朋友证号”。例如 123 和 51 就是朋友数,因为 1+2+3 = 5+1 = 6,而 6 就是它们的朋友证号。给定一些整数,要求你统计一下它们中有多少个不同的朋友证号。

思路

水~

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	map<int, bool> mp;
	for (int i = 0, x; i< n; ++i) {
		cin >> x;
		auto cal = [](int x) {
			int res = 0;
			while (x) {
				res += x % 10;
				x /= 10;
			}
			return res;
		};
		mp[cal(x)] = true;
	}
	cout << mp.size() << '\n';
	bool flag = false;
	for (auto e : mp) {
		if (flag) cout << ' ';
		flag = true;
		cout << e.first;
	}
	cout << '\n';
	return 0;
} 

HINT

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

发布了71 篇原创文章 · 获赞 15 · 访问量 3242

猜你喜欢

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