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 <iostream>
#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(auto it = s.begin(); it != s.end(); it++) {if(it != s.begin()) printf(" ");printf("%d", *it);}return 0;
}

1120. Friend Numbers (20)-PAT甲级真题相关推荐

  1. 1042. Shuffling Machine (20)-PAT甲级真题

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  2. 1081. Rational Sum (20)-PAT甲级真题

    Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...

  3. 1061. Dating (20)-PAT甲级真题

    Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akD ...

  4. 1108. Finding Average (20)-PAT甲级真题

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  5. 1077. Kuchiguse (20)-PAT甲级真题

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  6. 1031. Hello World for U (20)-PAT甲级真题

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  7. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  8. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

  9. PAT甲级真题 1011 World Cup Betting (20分) C++实现

    题目 With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly exc ...

最新文章

  1. Android单元測试之JUnit
  2. OpenJudge计算概论-找和为K的两个元素
  3. 2019年招聘过程的种种酸甜苦辣历程
  4. FlashBuilder 4.5 安装图解
  5. element元素 取属性_js element类型的属性和方法整理
  6. Adobe CS3教程安装问题
  7. 怎么用ps整合html图片,如何用PS把两张图片合并在一起?
  8. IDEA 里 VersionController 中的 log 中的不同颜色含义
  9. 基于stm32f103的俄罗斯方块游戏
  10. IO缓冲区(buffer)的原理及作用
  11. html课堂考勤系统源码,考勤管理系统课程设计源码
  12. 压缩软件大比拼历史回顾:ZIP与RAR
  13. 一分钟实现动态模糊效果
  14. 机器学习之R语言caret包trainControl函数(控制调参)
  15. 细说内网横向工具WMIHACK
  16. 当前的程序自动修复(automated program repair)工具介绍
  17. Java JDK 控制台环境设置
  18. WPLSoft PLC(可编程逻辑控制器)——应用指令
  19. 基于51单片机的多功能洗碗机(Protues仿真)
  20. PHP快速开发工具箱:轻松解决PHP日常编程问题的100个插件,在线

热门文章

  1. 表单元素值获取方式js及java方式
  2. linxu 启动过程分析
  3. 整理几个有想法的面试题
  4. 编程修养 阅读笔记二
  5. yii2框架中整合PHPOffice的PhpSpreadsheet开源库
  6. 面试总结(sohu)
  7. spring整合ehcache
  8. lnmp 虚拟主机的配置
  9. An internal error occurred during: Android Library Update.
  10. 软件定义重划边界——IT就是把复杂东西简单化