1. 程式人生 > >1120 Friend Numbers (20 分)

1120 Friend Numbers (20 分)

HERE test freopen algo clas UNC spa cat script

1120 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 frind 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

題意:統計數的各位數字之和,並升序輸出

分析:水題
 1 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-27-21.33.59
 6 * Description : A1120
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10
#include<iostream> 11 #include<cmath> 12 #include<algorithm> 13 #include<string> 14 #include<unordered_set> 15 #include<map> 16 #include<vector> 17 #include<set> 18 using namespace std; 19 const int maxn=10010; 20 set<int> ans; 21 int main(){ 22 #ifdef ONLINE_JUDGE 23 #else 24 freopen("1.txt", "r", stdin); 25 #endif 26 int n; 27 cin>>n; 28 string str; 29 for(int i=0;i<n;i++){ 30 cin>>str; 31 int sum=0; 32 int len=str.size(); 33 for(int j=0;j<len;j++){ 34 sum+=str[j]-0; 35 } 36 ans.insert(sum); 37 } 38 printf("%d\n",ans.size()); 39 for(auto it=ans.begin();it!=ans.end();it++){ 40 if(it!=ans.begin()) printf(" %d",*it); 41 else printf("%d",*it); 42 } 43 return 0; 44 }




1120 Friend Numbers (20 分)