A1120. Friend Numbers

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. Note: a number is considered a friend of itself.

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 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 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string>
 5 using namespace std;
 6 int hashTB[1000] = {0,0};
 7 string str;
 8 void str2num(string ss){
 9     int len = ss.length();
10     int ans = 0;
11     for(int i = 0; i < len; i++){
12         ans += (ss[i] - '0');
13     }
14     hashTB[ans]++;
15 }
16 int main(){
17     int N, cnt = 0;
18     cin >> N;
19     for(int i = 0; i < N; i++){
20         cin >> str;
21         str2num(str);
22     }
23     for(int i = 0; i < 1000; i++){
24         if(hashTB[i] > 0)
25             cnt++;
26     }
27     printf("%d\n", cnt);
28     int pt = 0;
29     for(int i = 0; i < 1000; i++){
30         if(hashTB[i] > 0){
31             pt++;
32             if(pt == cnt)
33                 printf("%d", i);
34             else printf("%d ", i);
35         }
36     }
37     cin >> N;
38     return 0;
39 }
View Code

总结:

1、注意不要理解错,一个数也可以是自己的朋友。所以一个数组成的和也算在输出范围内。

posted @ 2018-03-04 01:42  ZHUQW  阅读(105)  评论(0编辑  收藏  举报