【PAT乙级】1064 朋友数

xiaoxiao2025-09-15  35

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

输入格式:

输入第一行给出正整数 N。随后一行给出 N 个正整数,数字间以空格分隔。题目保证所有数字小于 10​4​​。

输出格式:

首先第一行输出给定数字中不同的朋友证号的个数;随后一行按递增顺序输出这些朋友证号,数字间隔一个空格,且行末不得有多余空格。

输入样例:

8

123 899 51 998 27 33 36 12

输出样例:

4

3 6 9 26

 个人理解 

水题略过

代码实现

#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <iostream> #define ll long long #define ep 1e-5 #define INF 0x7FFFFFFF using namespace std; const int maxn = 40; int main() { int n, cnt = 0; bool friends[maxn]; memset(friends, 0, sizeof(friends)); cin >> n; while (n --) { string num; cin >> num; int friend_num = 0; for (int i = 0; i < num.length(); i ++) { friend_num += num.at(i)-'0'; } if (!friends[friend_num]) { cnt ++; friends[friend_num] = true; } } cout << cnt << endl; for (int i = 0; i < maxn; i ++) { if (friends[i]) { cout << i; cnt --; if (cnt > 0) { cout << " "; } } } return 0; }

总结

学习不息,继续加油

 

转载请注明原文地址: https://www.6miu.com/read-5036354.html

最新回复(0)