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. C++ 构造函数的对象初始化列表

    //构造函数的对象初始化列表 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class P ...

  2. nib文件的默认搜索规则

    if you do not specify a nib name, and do not override the loadView method in your custom subclass, t ...

  3. 编程之美 set 1 不要被阶乘吓倒

    总结 1. 使用加法解决指数问题时, 可用背包问题的变形 2. 题目用到的公式和求解 1~N 中 1 出现的次数的公式类似 题目 1. 给定一个整数 N, 那么 N 的阶乘 N! 末尾有多少个 0 呢 ...

  4. input的disable和readonly

    在设计网页时,有时需要将输入框设置为只读状态,即其中的内容不可编辑,实现这种设计的方法有两种:使用input的disable和readonly两个属性. 先来看下二者的区别: <input ty ...

  5. Android开源项目分类汇总【畜生级别】

    From :http://blog.csdn.net/forlong401/article/details/25459403?c=6c4cd677a617db4655988e41ee081691#t7 ...

  6. 一些VS2013的使用技巧(转载)

    1. Peek View 可以在不新建TAB的情况下快速查看.编辑一个函数的代码. 用法:在光标移至某个函数下,按下alt+F12. 然后在Peek窗口里可以继续按alt+F12.然后按ctrl+al ...

  7. Java使用Apache POI进行Excel导入和导出

    Manve依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> ...

  8. centos免密登录

    本文是为了docker-machine增加现有虚拟机服务器为节点而做 docker-machine create -d generic --generic-ip-address=192.168.102 ...

  9. Python--进阶处理8

    # ====================第八章:类与对象========================= # --------------改变对象的字符串显示------------------ ...

  10. vscode编辑器配置C语言编译运行环境

    1.安装C/C++插件 2.安装编译环境,这里选择MinGW(http://mingw.org/ ) 选择一个安装目录,如:E:\workspace\MinGW mingw32-gcc开头的(包括了m ...