Two integers are called "friend numbers" if they share the same sum of their digits, and the sum is their "friend ID". For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID's among them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then N positive integers are given in the next line, separated by spaces. All the numbers are less than 104.
Output Specification:
For each case, print in the first line the number of different friend ID's among the given integers. Then in the second line, output the friend ID's in increasing order. The numbers must be separated by exactly one space and there must be no extra space at the end of the line.
Sample Input:
8
123 899 51 998 27 33 36 12
Sample Output:
4
3 6 9 26
思路:输入n个字符串,对字符串的每一位转成数字求和,最后存入到set种去重(默认按照从小到大)最后按照题要求格式输出(最后一个数字后面不能有空格)
#include<bits/stdc++.h>
using namespace std;
set<int>st;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
//cin.ignore();
string s;
while(n--){
cin>>s;
int len=s.size();
int sum=0;
for(int i=0;i<len;i++){
sum+=(s[i]-'0');
}
st.insert(sum);
}
cout<<st.size()<<'\n';
for(auto it=st.begin();it!=st.end();it++){
if(it!=st.begin()) cout<<" ";
cout<<*it;
}
return 0;
}