1120 Friend Numbers (20 分)

Posted mered1th

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1120 Friend Numbers (20 分)相关的知识,希望对你有一定的参考价值。

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
",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 分)的主要内容,如果未能解决你的问题,请参考以下文章

1120 Friend Numbers (20 分)难度: 简单 / 知识点: 哈希表

PAT甲级1120 Friend Numbers (20分)(SET遍历)

PAT 1120 Friend Numbers[简单]

1120 Friend Numbers (20)

PAT甲级——A1120 Friend Numbers20

PAT1120: Friend Numbers