当前位置:
首页 >
PAT甲级1120 Friend Numbers:[C++题解]set去重且有序
发布时间:2025/4/5
57
豆豆
生活随笔
收集整理的这篇文章主要介绍了
PAT甲级1120 Friend Numbers:[C++题解]set去重且有序
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
文章目录
- 题目分析
- 题目链接
题目分析
来源:acwing
对于每个数,统计各位之和,判断有多少个不同的和,然后从小到大输出。
什么数据结构既能去重,又能排序呢?
答案就是set!!!
关于set的介绍
在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。
引用自:https://blog.csdn.net/yas12345678/article/details/52601454
ac代码
#include<bits/stdc++.h> using namespace std; const int N = 1e4+10; int n; set<int> S;int main(){cin >> n;for(int i = 1; i<= n; i++){int x ;int s=0;cin >> x;while(x){s += x%10,x /=10;}S.insert(s);}cout << S.size()<<endl;bool is_first = true;//范围遍历set,set本身有序,且去重for(auto x : S){if(is_first) is_first = false;else cout<<" ";cout << x;}}题目链接
PAT甲级1120 Friend Numbers
https://www.acwing.com/problem/content/1612/
总结
以上是生活随笔为你收集整理的PAT甲级1120 Friend Numbers:[C++题解]set去重且有序的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: PAT甲级1063 Set Simila
- 下一篇: PAT甲级1144 The Missin