PTA Friend Numbers (20分)

释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间。

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 10​4​​.

Output Specification:

For each case, print in the first line the number of different frind 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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX

//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1010;
int N,s[10010],ct;
set<int> jg;
int sum(int x){
    int ans=0;
    while(x){
        int ls=x%10;
        ans+=ls;
        x/=10;
    }
    return ans;
}
int main(){
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>s[i];
        jg.insert(sum(s[i]));
    }
//    for(int i=0;i<N;i++){
//        for(int j=i+1;j<N;j++){
//            if(sum(s[i])==sum(s[j])){
//                ct++;
//                jg.insert(sum(s[i]));
//            }
//        }
//    }
    set<int>::iterator i;
    cout<<jg.size()<<endl;
    for(i=jg.begin();i!=jg.end();i++){
        if(i!=jg.begin())
            cout<<' ';
            cout<<*i;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/108481332