PAT 1120 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 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 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
题目大意:两个数如果每位相加结果相等,那么结果就是个友好数,现在给出几个数,求他们的友好数。
//就算一个数没有和别的单位数和一样的,也将这个算作友好数。
//我的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[简单]的更多相关文章
- PAT 1120 Friend Numbers
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the sa ...
- pat 1120 Friend Numbers(20 分)
1120 Friend Numbers(20 分) Two integers are called "friend numbers" if they share the same ...
- PAT甲级 1120. Friend Numbers (20)
1120. Friend Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Two in ...
- 1120 Friend Numbers (20 分)
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
- 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 ...
- 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 ...
- PAT甲题题解-1120. Friend Numbers (20)-水题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789775.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- [PAT]A+B Format[简单]
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT 1041 Be Unique[简单]
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
随机推荐
- EasyUI 异步Tree
用etmvc framework返回json数据.创建HTML标记 <ul id="tt"></ul> 创建jQuery代码我们使用url属性来指向远程数据 ...
- 数据库 数据库SQL语句四
多表查询 等值连接 --查询员工信息,员工号,姓名,月薪,部门名称 select e.empno,e.ename,d.dname from emp e,dept d where e.deptno=d. ...
- redhat6.2 clang编译环境搭建(采用源码包编译安装)
1. About clang++ office site:http://clang.llvm.org/ A major focus of our work on clang is to make it ...
- 数据透视表sql:用SQL行列转换实现数据透视的一些思考
用SQL行列转换实现数据透视的一些思考 摘要:根据对报表开发过程中碰到的需要用SQL行列转换进行解决的一类查询统计问题的分析,逐步探索求解得到一种较通用的解决思路,并用函数进行实现.该解决思路及函数实 ...
- CDialog与CDialogEx的区别联系
CDialogEx是VS2003之后出现的,VC++6.0没有.CDialogEx = CDialog ExtendExtend的意思是扩展,即扩展的CDialog! 这个类是CDialog的扩展类, ...
- AWS系列-复制AMI到其他区域
复制AMI镜像到其他区域 1.1 打开EC2控制面板,找到AMI 选择需要迁移的AMI 1.2 选择复制的目标区域 选择复制到目标区域 我这里是 东京复制到新加坡 1.3 点击完成后,切换到相应的区域 ...
- Windows下安装Apache 2.2.21图文教程
https://www.jb51.net/article/52086.htm 本文详细介绍了在Windows平台上安装Apache的过程,希望对初次安装Apache的朋友有所帮助. 1. 软件准备 我 ...
- 分页技巧__设计分页用的QueryHelper辅助对象
分页技巧__设计分页用的QueryHelper辅助对象 FROM 必须 实体名 WHERE 可选 条件1 AND 条件2 AND ... 条件n ORDER BY 可选 属性1, 属性2, ... ...
- Javascript实现图片的预加载的完整实现
图片预加载是web开发中一种应用相当广泛的技术,比如我们在做图片翻转显示等特效的时候,为了让图片在转换的时候不出现等待,我们最好是先让图片下载到本地,然后在继续执行后续的操作.今天我们将来实现一个完整 ...
- 【BZOJ1898】[Zjoi2005]Swamp 沼泽鳄鱼 矩阵乘法
[BZOJ1898][Zjoi2005]Swamp 沼泽鳄鱼 Description 潘塔纳尔沼泽地号称世界上最大的一块湿地,它地位于巴西中部马托格罗索州的南部地区.每当雨季来临,这里碧波荡漾.生机盎 ...