PAT甲级——A1120 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 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
 1 #include <iostream>
 2 #include <set>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     string s;
 9     set<int>res;
10     cin >> n;
11     while (n--)
12     {
13         cin >> s;
14         int sum = 0;
15         for (auto a : s)
16             sum += a - ‘0‘;
17         res.insert(sum);
18     }
19     cout << res.size() << endl;
20     for (auto a : res)
21         cout << (a == *(res.begin()) ? "" : " ") << a;
22     cout << endl;
23     return 0;
24 }

原文地址:https://www.cnblogs.com/zzw1024/p/11469941.html

时间: 2024-08-08 18:11:27

PAT甲级——A1120 Friend Numbers【20】的相关文章

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a

PAT 甲级 1120 Friend Numbers

1 #include <iostream> 2 #include <cstring> 3 int a[10000]; 4 int max1=0, n=0; 5 int first = 0; 6 using namespace std; 7 8 void bijiao() 9 { 10 int i; 11 for (i=1;i<=max1; i++) 12 { 13 if (a[i]>=1) 14 { 15 n++; 16 } 17 } 18 } 19 void shuc

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

【PAT甲级】1008 Elevator (20分)

1008 Elevator 题目: The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one

PAT甲级——1073 Scientific Notation (20分)

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9].[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at lea

【PAT甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if

【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. 代码: #include<bits/stdc++.h>using namespace std;string s[107];int length[107];char ans[307];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; cin.igno

PAT 甲级 1069 The Black Hole of Numbers (20 分)(内含别人string处理的精简代码)

1069 The Black Hole of Numbers (20 分) For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second