1120 Friend Numbers (20 分)——C/C++(set用法)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了1120 Friend Numbers (20 分)——C/C++(set用法),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1593字,纯文字阅读大概需要3分钟。
内容图文
![1120 Friend Numbers (20 分)——C/C++(set用法)](/upload/InfoBanner/zyjiaocheng/600/b71dafdf1fee404ca8080cf13b92a635.jpg)
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.
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
题目大意:
给定n个数字,并把每位数字相加的和按从小到大的顺序输出,重复的输出一个。
分析及思路:
使用set集合,set会自动把集合中的数据从小到大排列,并且不会有重复数字。但set中能用迭代器遍历,这样最后一个空格会非常不好掌握,可用数组中转一下。
AC代码:
#include<iostream>
#include<set>
using namespace std;
int a[10010];
int main(){
int n, i = 0;
cin >> n;
set<int> ss;
for(int i = 0; i < n; i++){
int temp, sum = 0;
cin >> temp;
while(temp > 0){
sum += temp % 10;
temp /= 10;
}
ss.insert(sum);
}
cout << ss.size() << endl;
set<int>::iterator it;
for(it = ss.begin(); it != ss.end(); it++) a[i++] = *it;
for(i = 0; i < ss.size() - 1; i++) cout << a[i] << ' ';
cout << a[i] << endl;
return 0;
}
内容总结
以上是互联网集市为您收集整理的1120 Friend Numbers (20 分)——C/C++(set用法)全部内容,希望文章能够帮你解决1120 Friend Numbers (20 分)——C/C++(set用法)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。