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 ...
随机推荐
- php 显示一个干净的,易被解析的json
header("Content-type: text/html; charset=utf-8"); //试着从数据库里读取一条数据放进来 $con = mysql_connect( ...
- 【BZOJ】1690: [Usaco2007 Dec]奶牛的旅行(分数规划+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1690 第一题不是水题的题.. 分数规划.. T-T 百度吧..http://blog.csdn.ne ...
- 如今就报名上海ORACLE用户组2014年高峰论坛吧!
COLLABORATE 14 – SHOUG Forum 上海ORACLE用户组2014年高峰论坛报名本次活动由ORACLE ACS高级服务部门与 SHOUG 上海ORACLE用户组合办. 大会议程包 ...
- adb server is out of date.killing的解决办法
当把手机连接到电脑端口运行adb程序调试时,出现了下面这样的情况: 分析:出错的原因是adb的端口被其他程序的进程占领了,所以要做的就是找到并kill该进程. 工具/原料 cmd.exe. ...
- 修改CFileDialog的标题
CFileDialog f(TRUE); f.m_ofn.lpstrTitle = "我的标题"; f.DoModal(); 设置标题! CFileDialog ...
- Target frameworks
https://docs.microsoft.com/en-us/dotnet/standard/frameworks When you target a framework in an app or ...
- 实现 IteratorAggregate接口
<?php class MyIterator implements Iterator{ private $var = array(); public function __construct($ ...
- jQuery源码分析-each函数
本文部分截取自且行且思 jQuery.each方法用于遍历一个数组或对象,并对当前遍历的元素进行处理,在jQuery使用的频率非常大,下面就这个函数做了详细讲解: 复制代码代码 /*! * jQuer ...
- iPhone 6 Screens Demystified
http://www.paintcodeapp.com/news/iphone-6-screens-demystified
- TFS 创建团队成员及管理
用TFS服务器管理代码 1:创建TFS服务器 2:创建TFS项目 3:创建TFS团队成员 4:上传代码,共享链接,对代码进行管理 这里主要介绍第三步:创建TFS团队成员 第一种:通过连接进入TFS可视 ...