码迷,mamicode.com
首页 > 其他好文 > 详细

1120 Friend Numbers (20 分)

时间:2019-02-28 01:10:03      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:stdin   cst   mes   水题   rip   rate   exactly   def   line   

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 分)

标签:stdin   cst   mes   水题   rip   rate   exactly   def   line   

原文地址:https://www.cnblogs.com/Mered1th/p/10447942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!