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 ...
 
随机推荐
- ubuntu系统AndroidStudio修改内存大小
			
位于android-studio/bin目录下的studio64.vmoptions和studio.vmoptions文件. 把Xms,Xmx,-XX:MaxPermSize,-XX:Reserved ...
 - 说说struts2中拦截器的请求流程一(模拟大致流程)
			
本文可作为北京尚学堂struts2课程的学习笔记. 首先 什么是拦截器?拦截器能干什么? 拦截器,顾名思义就是拦截对象然后做操作的东西,至于是拦截谁?那自然是拦截action了.能做什么操作呢?你想让 ...
 - Android进阶(十九)AndroidAPP开发问题汇总(三)
			
Android进阶(十九)AndroidAPP开发问题汇总(三) Java解析XML的几种方式: http://inotgaoshou.iteye.com/blog/1012188 从线程返回数据的两 ...
 - Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片
			
Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...
 - RTMPdump(libRTMP) 源代码分析 6: 建立一个流媒体连接 (NetStream部分 1)
			
===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...
 - JSP 知识基本
			
from:http://blog.csdn.net/caipeichao2/article/details/38589293 more:http://www.2cto.com/kf/web/jsp/4 ...
 - 如果去掉UITableView上的section的headerView和footerView的悬浮效果
			
项目需要cell的间距,又不需要悬浮效果,百度之后找到这个方法,记录一下,备忘. 用UIScrollView的代理方法实现 - (void)scrollViewDidScroll:(UIScrollV ...
 - SharePoint 2007 单列表模糊查询SPD定制
			
应用场景:项目中总会遇到一些列表,存着是用户.项目等数据,而我们需要查询有哪些项目,这时候,就需要用到模糊查询了,而这样的查询,基本不需要跨列表,所以,也没必要配置复杂的搜索,用Designer(简称 ...
 - RubyMotion之父:Ruby是目前替代Objective-C的最佳iOS开发语言
			
发表于2012-08-16 00:52| 21716次阅读| 来源CSDN| 24 条评论| 作者杨鹏飞 RubyMotionRubyObjective-CiOSJava 摘要:曾几何时,PC端有那么 ...
 - 负载均衡之让nginx跑起来
			
一个简单的原因,我不得不考虑负载 小源做了个网站,很简单,传统的java开放框架,和一个tomcat搞定,让人没想到的是网站既然火起来了,很快一个tomcat就搞不定了,怎么办? 网站访问量很大,既然 ...