PAT (Advanced Level)1120. Friend Numbers (20)
2018-03-12 15:11
363 查看
1120. Friend Numbers (20)
时间限制400 ms内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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 12Sample Output:
4 3 6 9 26
怎么办,我这水平就20分的题能做的比较顺
//1120. Friend Numbers(20)
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
int n, s[50] = { 0 }, x[50] = { 0 }, cnt = 0;
cin >> n;
string a;
while (n--) {
int sum = 0;
cin >> a;
for (int i = 0; i < a.length(); i++) {
sum += a[i] - '0';
}
s[sum] = 1;
}
for (int i = 0; i < 50; i++) {
if (s[i] != 0)
x[cnt++] = i;
}
sort(x, x + cnt);
cout << cnt << endl;
cout << x[0];
for (int i = 1; i < cnt; i++)
cout << " " << x[i];
return 0;
}
相关文章推荐
- PAT (Advanced Level) Practise 1120 Friend Numbers (20)
- PAT (Advanced Level) Practise 1120 Friend Numbers (20)
- 【PAT】【Advanced Level】1120. Friend Numbers (20)
- 浙大 PAT Advanced level 1015. Reversible Primes (20)
- 1092. To Buy or Not to Buy (20)【水题】——PAT (Advanced Level) Practise
- 浙大PAT (Advanced Level) Practise 1008 Elevator (20)
- PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)
- PAT (Advanced Level) 1092. To Buy or Not to Buy (20)
- 1023. Have Fun with Numbers (20)【字符串操作】——PAT (Advanced Level) Practise
- 【PAT Advanced Level】1023. Have Fun with Numbers (20)
- 【PAT Advanced Level】1031. Hello World for U (20)
- PAT (Advanced Level)1015. Reversible Primes (20)
- 1008. Elevator (20)——PAT (Advanced Level) Practise
- PAT (Advanced Level) 1046. Shortest Distance (20) 求环中两点之间的距离
- 【c++】PAT (Advanced Level) 1005. Spell It Right (20)
- PAT (Advanced Level) 1088. Rational Arithmetic (20) 分数四则运算
- PAT (Advanced Level) Practise 1120
- PAT (Advanced Level) Practise 1031 Hello World for U (20)
- PAT-PAT (Advanced Level) Practise 1001. A+B Format (20) 【二星级】
- PAT (Advanced Level) Practise 1088 Rational Arithmetic (20)