1120. Friend Numbers (20)-PAT甲级真题
来源:互联网 发布:python输出日志文件 编辑:程序博客网 时间:2024/05/18 01:09
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 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 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
分析:在接收输入数据的时候就把该数字的每一位相加,并把结果插入一个set集合中。因为set是有序的、不重复的,所以set的size值就是输出的个数,set中的每一个数字即所有答案的数字序列
此为C/C++版,Java版本请戳:http://www.liuchuo.net/archives/2736
#include <cstdio>#include <set>using namespace std;int getFriendNum(int num) { int sum = 0; while(num != 0) { sum += num % 10; num /= 10; } return sum;}int main() { set<int> s; int n, num; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &num); s.insert(getFriendNum(num)); } printf("%d\n", s.size()); for(set<int>::iterator it = s.begin(); it != s.end(); it++) { if(it != s.begin()) printf(" "); printf("%d", *it); } return 0;}
- 1120. Friend Numbers (20)-PAT甲级真题
- 1120. Friend Numbers (20)-PAT甲级真题
- PAT - 甲级 - 1120. Friend Numbers (20)
- 1120. Friend Numbers (20) PAT 甲级
- PAT甲级 1120. Friend Numbers (20)
- PAT 甲级 1120. Friend Numbers (20)
- 1100. Mars 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)
- 1023. Have Fun with Numbers (20)-PAT甲级真题(大整数运算)
- 1069. The Black Hole of Numbers (20)-PAT甲级真题
- PAT A1120 friend numbers (20)
- PAT A1120. Friend Numbers (20)
- 1008. Elevator (20)-PAT甲级真题
- 1061. Dating (20)-PAT甲级真题
- 如何防止非公开内容被搜索引擎搜到?
- 谁有m55啊?帮忙测试一下我的这个游戏是否能在m55上运行。高分相送!
- 如何在下次登陆时显示上次成功登陆后的账号的密码
- 第十五节:矩阵键盘单个触发的压缩代码编程
- 求教 修改以前的ASP网站遇到问题 关于conn.execute
- 1120. Friend Numbers (20)-PAT甲级真题
- 第十六节:矩阵键盘的组合按键触发
- 为什么程序运行后在安装列表中找不到,新手求教,谢谢
- 求助:分页代码的错误解决方法
- 无法下载Nokia Developer's Suite 2.2 for J2ME
- 在EVC中的图像操作问题
- 第十七节:两片联级74HC595驱动16个LED灯的基本驱动程序
- 该怎样配置JB80.1 .mobileset3.0.1和nokia呢?
- 第十八节:把74HC595驱动程序翻译成类似单片机IO口直接驱动的方式