PAT A1120 Friend Numbers
程序员文章站
2022-04-15 14:37:18
...
PAT A1120 Friend Numbers
Sample Input:
8
123 899 51 998 27 33 36 12
Sample Output:
4
3 6 9 26
- 思路 1:
- 求出每个输入数各位数的和,insert入set
- 遍历set输出
- code 1:
#include <iostream>
#include <set>
using namespace std;
set<int> se;
int digSum(int x){
int ans = 0;
do{
ans += x % 10;
x /= 10;
}while(x > 0);
return ans;
}
int main(){
int n, tmp, cnt = 0;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &tmp);
se.insert(digSum(tmp));
}
printf("%d\n", se.size());
for(auto it = se.begin(); it != se.end(); ++it){
cout << *it;
if(++cnt < se.size()) printf(" ");
}
return 0;
}
上一篇: Set集合
下一篇: L1-033 出生年
推荐阅读
-
【PAT】A1069 The Black Hole of Numbers (20分)
-
PAT(A)1069 The Black Hole of Numbers (20分)
-
pat-1069 The Black Hole of Numbers (20分)
-
[PAT-A 1069]The Black Hole of Numbers
-
PAT-A-1069 The Black Hole of Numbers 【验证】
-
PTA(A)1120 Friend Numbers (20分)
-
PAT A1120 Friend Numbers
-
PAT A1069 The Black Hole of Numbers (20分)