备战蓝桥杯-牛客
- 软件开发
- 2025-08-24 04:57:01

习题-[NOIP2006]明明的随机数
1046-习题-[NOIP2006]明明的随机数_2021秋季算法入门班第一章习题:模拟、枚举、贪心
思路:这道题用stl的set,今天写这道题复习了一下set的用法:
s.find(a) == s.end()的意思是判断元素a是否存在于集合s中。 如果s.find(a)返回的迭代器等于s.end()的迭代器,说明元素a不存在于集合s中,返回true; 否则返回false。 s.find(a)==s.end()是表示a不存在set中 s.insert(op);//添加这个元素到set中 s.size();//返回集合的个数
用迭代器实现set的遍历
for (auto it = st.begin();it != st.end();it++) { std::cout << *it << " "; }ac代码:
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #include <utility> #include <set> #define int long long const int N = 1e5 + 10; int a[N]; signed main() { std::set<int>st; int n; std::cin >> n; for (int i = 1;i <= n;i++) { int x; std::cin >> x; if (st.find(x) == st.end()) { st.insert(x); } else if(st.find(x)==st.end()) { continue; } } std::cout << st.size() << "\n"; for (auto it = st.begin();it != st.end();it++) { std::cout << *it << " "; } return 0; }