PAT1120: Friend Numbers
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 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 思路 水题,用个map记录数字的每一位数相加的和就行,map自动升序排序,所以直接遍历map输出就行。 代码
#include<iostream>
#include<map>
#include<iterator>
using namespace std; int main()
{
int N;
while(cin >> N)
{
map<int,int> dic;
for(int i = ;i < N;i++)
{
int number,sum = ;
cin >> number;
while(number != )
{
sum += (number % );
number /= ;
}
dic.insert(pair<int,int>(sum,));
} cout << dic.size() << endl;
map<int,int>::iterator it = dic.begin();
cout << it++->first;
for(; it != dic.end();it++ )
{
cout << " " << it->first;
}
cout << endl;
}
}
PAT1120: Friend Numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- Leetcode_144_Binary Tree Preorder Traversal
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876699 Given a binary tree, r ...
- Android表格布局(Table Layout)
Android表格布局(Table Layout) 先来看布局管理器之间继承关系图: 图1 可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器. 表格布局采用 ...
- Android Studio 1.2.2设置显示行号
Android Studio设置显示行号的方法与Eclipse有少许差别,直接在代码中右键,弹出右键菜单是没有显示行号功能的. 在Android Studio中设置方法有二: 1.临时显示行号 在单个 ...
- gcc学习(一)[第二版]
gcc简介 1. gcc是GNU Compiler Collection的缩写.最初是作为C语言的编译器(GNU C Compiler),作者为Richard Stallman,是GNU项目的奠基者 ...
- 浅析数据结构中栈与C实现
最近在搞摄像头驱动,o()︿︶)o 唉,别提有多烦,一堆寄存器就有人受的了--特么这不是单片机的开发,这是内核驱动开发-- 今天放松一下,我们来看看数据结构中的栈,这节的知识点可以说是数据结构中最容易 ...
- java面试之常见编程题
1.编程实现:二分搜索算法 解答: public class SearchTest { /** 被搜索数据的大小 */ private static final int size = 5000000; ...
- LIRe 源代码分析 4:建立索引(DocumentBuilder)[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- 避免"Physics Space Locked"错误
在一些cocos2d中使用物理引擎的代码中,往往会出现如下错误: Aborting due to Chipmunk error: You cannot manually reindex objects ...
- Oracle ERP系統借贷关系表
系统分步骤产生的分录: 1)库存模块作接收时产生的分录为: 借:材料采购 (采购单价X订单数量) 贷:应计负债 (采购单价X订单数量) 2)库存模块作检验入库时产生的分录为: 系统产生的分录分别为: ...
- C语言笔试经典-查找多位数重复数字以及次数
从键盘输入一个多位的整数 用程序判断 这个数里面有没有 重复的数字 有重复的数字就打印 哪个数字重复了 重复了几次 例如:输入:1122431 打印结果: 1重复 出现3次 2重复 出现2次, ...