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 ...
随机推荐
- IOS微信API异常:unrecognized selector sent to instance 0x17005c9b0'
开发IOS整合微信API的时候,在运行程序的过程中可能会在注册你的APPID的时候抛出此异常而导致程序崩溃. 异常描述 [7661:2826851] *** Terminating app due t ...
- virtualbox pxe启动
在设置——系统中更改启动顺序,优先使用网路启动,如果你的网络环境支持PXE启动,就可以使用网络安装了. 转自: http://blog.csdn.net/huanghuibai/article/det ...
- Spring RestTemplate post
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("auditPara ...
- Visual Studio Code调试node.js:无法在PATH上找到运行时的node
首先,环境变量Path中加入nodejs的路径: 验证nodejs是否已经加入环境变量: 接着,重新启动Visual Studio Code, 试一下,是不是好了~ 附录:Visual Studi ...
- AWS CLI 【S3】
1.创建一个桶&删除一个桶 root@syavingc:~# aws s3 mb s3://syavingc #创建一个桶 make_bucket: syavingc root@syaving ...
- Libcap的简介及安装
Libpcap 简介 libpcap 是unix/linux 平台下的网络数据包捕获函数包, 大多数网络监控软件都以它为基础. Libpcap 可以在绝大多数类unix 平台下工作. Libpcap ...
- UVA 548(二叉树重建与遍历)
J - Tree Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Ap ...
- 工作流JBPM_day01:1-说明_MyProcessDesigner_流程设计器
工作流JBPM_day01:1-说明 先只做请假功能,怎么做? (请假可以和考勤整合到一起) 1,银行(拿号---叫号---办理) 2,餐馆(点菜---上菜---结账) 3,网购(下订单--配送--收 ...
- FAIL : SSHException: Incompatible ssh peer (no acceptable kex algorithm)问题解决及更新paromiko失败问题解决
转自:http://blog.csdn.net/qy924565830/article/details/52164256 http://blog.csdn.net/coder_xia/article/ ...
- ios模拟器键盘不弹出
本文转载至 http://blog.csdn.net/cerastes/article/details/39547967 xcode6键盘iPhone-Portrait-Numb不弹出Cant fin ...