思路:
set 内部自动有序且不含重复元素的容器。非常适合本题。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10003;
int n, all = 0;
int num[maxn];
set<int> ans;
int main() {
cin>>n;
for (int i = 0; i < n; i++) {
cin>>num[i];
int sum = 0;
while (num[i] != 0) {
sum += num[i] % 10;
num[i] /= 10;
}
ans.insert(sum);
}
cout<<ans.size()<<endl;
for (auto it = ans.begin(); it != ans.end(); it++) {
if (it == ans.begin()) ;
else cout<<" ";
cout<<*it;
}
}
网友评论