【PAT】【Advanced Level】1120. Friend Numbers (20)
来源:互联网 发布:ipcms录像软件 编辑:程序博客网 时间:2024/06/10 01:55
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 friend ID's among them. Note: a number is considered a friend of itself.
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:8123 899 51 998 27 33 36 12Sample Output:
43 6 9 26
https://www.patest.cn/contests/pat-a-practise/1120
思路:
输入,统计,map映射去重。
CODE:
#include<iostream>#include<map>#include<vector>#include<algorithm>using namespace std;map<int,int> ma;vector<int> res;int main(){int n;cin>>n;for (int i=0;i<n;i++){int t;cin>>t;int sum=0;while (t>0){sum+=t%10;t/=10;}if (ma[sum]==0){ma[sum]=1;res.push_back(sum);}}sort(res.begin(),res.end());cout<<res.size()<<endl;for (int i=0;i<res.size();i++){if (i!=0) cout<<" ";cout<<res[i];}return 0;}
- 【PAT】【Advanced Level】1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120.Friend Numbers
- PAT (Advanced Level) Practise 1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120. Friend Numbers (20)
- PAT (Advanced Level) Practise 1120Friend Numbers (20)
- PAT (Advanced Level) Practise 1120 Friend Numbers (20)
- 【PAT】【Advanced Level】1100. Mars Numbers (20)
- PAT--1120. Friend Numbers (20)
- 【PAT】1120. Friend Numbers
- pat 1120. Friend Numbers
- PAT - 甲级 - 1120. Friend Numbers (20)
- PAT(A)- 1120. Friend Numbers (20)
- 1120. Friend Numbers (20) PAT 甲级
- PAT甲级 1120. Friend Numbers (20)
- PAT 甲级 1120. Friend Numbers (20)
- 1023. Have Fun with Numbers (20) @ PAT (Advanced Level) Practise
- 【PAT Advanced Level】1023. Have Fun with Numbers (20)
- 加强交互设计过程的「逻辑性」,能解决哪些问题
- 纽约生活: 最让人喜爱的纽约蛋糕店
- 报错Test cannot be resolved to a type
- 图解http(03)-http首部
- 关于TSnackbar遇到的一个坑
- 【PAT】【Advanced Level】1120. Friend Numbers (20)
- 幸运号码 数位DP
- Android log到保存文件
- 使用JavaScript实现广告轮播特效
- 【大话运维·第7期】浅谈一体化智能监控运维
- JSONModel对架构的影响及解决方案
- 目标检测(一)--Objectness算法总体理解,整理及总结
- DeepLearning&Tensorflow学习笔记4__mnist数据集DCGAN
- Android Sip学习(四)Android自带SipDemo详解