PAT_A1120#Friend Numbers

Source:

PAT A1120 Friend Numbers (20 分)

Description:

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 1.

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

Keys:

  • 简单模拟

Code:

 1 /*
 2 Data: 2019-08-14 16:21:28
 3 Problem: PAT_A1120#Friend Numbers
 4 AC: 13:24
 5 
 6 题目大意:
 7 数字的各位和称为朋友ID,求有多少个朋友ID
 8 */
 9 #include<cstdio>
10 #include<set>
11 #include<string>
12 #include<iostream>
13 using namespace std;
14 
15 int main()
16 {
17 #ifdef ONLINE_JUDGE
18 #else
19     freopen("Test.txt", "r", stdin);
20 #endif // ONLINE_JUDGE
21 
22     int n,cnt=0;
23     string s;
24     set<int> st;
25     scanf("%d", &n);
26     for(int i=0; i<n; i++)
27     {
28         cin >> s;
29         cnt=0;
30         for(int j=0; j<s.size(); j++)
31             cnt += s[j]-'0';
32         st.insert(cnt);
33     }
34     cnt=0;
35     printf("%d\n", st.size());
36     for(auto it=st.begin(); it!=st.end(); it++)
37         printf("%d%c", *it,++cnt==st.size()?'\n':' ');
38 
39     return 0;
40 }

 

posted @ 2019-05-29 21:57  林東雨  阅读(161)  评论(0编辑  收藏  举报