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 frind ID's among them.

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

题目大意:两个数如果每位相加结果相等,那么结果就是个友好数,现在给出几个数,求他们的友好数。

//就算一个数没有和别的单位数和一样的,也将这个算作友好数。

//我的AC

#include <iostream>
#include <map>
using namespace std; int main() {
int n;
map<int,int> mp;
cin>>n;
int t;
for(int i=;i<n;i++){
cin>>t;
int x,sum=;
while(t!=){
x=t%;
t/=;
sum+=x;
}
mp[sum]=;
sum=;
}
cout<<mp.size()<<'\n';
for(auto it=mp.begin();it!=mp.end();){
cout<<it->first;//每次都不知道这里的空格该怎么控制?!!!
if(++it!=mp.end())
cout<<" ";
}
return ;
}

//还是比较简单的,就是用map;

在解决最后输出格式的问题上,可以使用一个if判断,注意应该是++it去判断,而不是it++,深刻地理解了这个前+和后+的区别。

PAT 1120 Friend Numbers[简单]的更多相关文章

  1. PAT 1120 Friend Numbers

    1120 Friend Numbers (20 分)   Two integers are called "friend numbers" if they share the sa ...

  2. pat 1120 Friend Numbers(20 分)

    1120 Friend Numbers(20 分) Two integers are called "friend numbers" if they share the same ...

  3. PAT甲级 1120. Friend Numbers (20)

    1120. Friend Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Two in ...

  4. 1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

  5. PAT 1069 The Black Hole of Numbers[简单]

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

  6. PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  7. PAT甲题题解-1120. Friend Numbers (20)-水题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789775.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. [PAT]A+B Format[简单]

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  9. PAT 1041 Be Unique[简单]

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

随机推荐

  1. css实现圆角三角形例子(无图片)

    css实现圆角三角形例子(无图片) 以前我们做圆角时都会要用到图片来实现,今天我给大家整理的这款css圆角效果是不需要图片的纯css实现的哦,下面我们一起来看看吧. 效果如下图所示 三角形所对方向&q ...

  2. 22SpringMvc_jsp页面上的数据传递到控制器的说明

    假设有这个一个业务:在jsp页面上写入数据,然后把这个数据传递到后台. 效果如下:

  3. php -- 对象遍历

    对象遍历:foreach,遍历对象的公有属性(将公有属性的值和属性名赋值给对应$value和$key) 遍历某一个属性的数组,实现Iterator接口 接口iterator:类实现Iterator接口 ...

  4. bootstrap基础学习二篇

    这篇学习网格系统(Grid System) Bootstrap 提供了一套响应式.移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 1.网格选项: ...

  5. adb server is out of date.killing的解决办法

    当把手机连接到电脑端口运行adb程序调试时,出现了下面这样的情况: 分析:出错的原因是adb的端口被其他程序的进程占领了,所以要做的就是找到并kill该进程.     工具/原料   cmd.exe. ...

  6. hrbustoj 1104:Leyni, LOLI and Line(解析几何,斜截式的应用)

    Leyni, LOLI and Line Time Limit: 1000 MS    Memory Limit: 65536 K Total Submit: 181(54 users)   Tota ...

  7. [转]Shell脚本中获取SELECT结果值的方法

    http://blog.itpub.net/13885898/viewspace-1670297/ 有时候我们可能会需要在Shell脚本中执行SELECT语句,并将结果赋值给一个变量,对于这样的情形, ...

  8. Python爬虫(五)

    源码: import requests from lxml import etree from my_mysql import MysqlConnect mc = MysqlConnect(','ho ...

  9. &lt; APT 攻击&gt;看起来是 .PPT 附件,竟是 .SCR !!

    趋势科技以前在2013年下半年度目标攻击综合报告里指出,发现了好几起APT攻击-高级持续性渗透攻击 (Advanced Persistent Threat, APT) /目标攻击相关的攻击活动. 趋势 ...

  10. iOS 开发之 -- UDID和UUID的详解

    老实说,搞了几年的ios开发了,对基础的概念,还是不牢固,整天都是为了赶进度而码代码,这里记录一下这两者的区别: UDID的全名为 Unique Device Identifier :设备唯一标识符. ...