PAT 甲级 1120. Friend Numbers (20)
来源:互联网 发布:天地图数据融合 编辑:程序博客网 时间:2024/06/05 03:35
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 12
Sample Output:
4
3 6 9 26
#include <cstdio>#include <set>using namespace std;int getFriendNum(int n){ int sum=0; while(n!=0) { sum+=n%10; n/=10; } return sum;}int main(){ set<int> s; int n,num; scanf("%d",&n); while(n--) { scanf("%d",&num); s.insert(getFriendNum(num)); } printf("%d\n",s.size()); for(set<int>::iterator it=s.begin();it!=s.end();it++){//set<int>::iterator是迭代器 if(it!=s.begin()) printf(" "); printf("%d",*it); } return 0;}
- PAT - 甲级 - 1120. Friend Numbers (20)
- 1120. Friend Numbers (20) PAT 甲级
- PAT甲级 1120. Friend Numbers (20)
- PAT 甲级 1120. Friend Numbers (20)
- 1120. Friend Numbers (20)-PAT甲级真题
- 1120. Friend Numbers (20)-PAT甲级真题
- PAT--1120. Friend Numbers (20)
- 【PAT】1120. Friend Numbers
- pat 1120. Friend Numbers
- PAT(A)- 1120. Friend Numbers (20)
- 【PAT】【Advanced Level】1120. Friend Numbers (20)
- PAT A1120 friend numbers (20)
- PAT A1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120. Friend Numbers (20)
- 1120. Friend Numbers (20)
- 1120. Friend Numbers (20)
- List add元素返回java.lang.NullPointerException
- PAT 甲级 1119. Pre- and Post-order Traversals (30)
- 程序的不同段的区别和作用
- unique-paths
- 在Windows下配置svn服务端钩子程序
- PAT 甲级 1120. Friend Numbers (20)
- 4、单例模式(静态代码块实现)
- 数据结构与算法之四(链表)
- PAT 甲级 1121. Damn Single (25)
- MongoDB权威指南--阅读笔记(一)
- nginx配置location总结及rewrite规则写法
- DDMS Data无法访问--解决方案
- java中String、StringBuffer、StringBuilder的区别
- 流水账笔记:PE文件格式(TLS)