跳到正文

题解

题解:P1059 [NOIP 2006 普及组] 明明的随机数

蒟蒻whker约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年8月8日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 1 分钟
获赞
3
题目
P1059

版本

  1. v1当前

STL 好闪,拜谢 STL。

发现 STL 自带的 set 自带去重和排序的功能,于是直接利用 set 的特性即可 AC。

本题要用到的 set 语法:

  • set<int>st 声明一个名为 stst 的整型 set。
  • st.insert(x) 向 stst 中插入 xx。
  • st.size() 返回 stst 的大小。

另外,本题还需要迭代器来遍历 set,如果不了解详见此处。

话不多说,上代码。

C++
#include <iostream>
#include <set>//记得引用set头文件
using namespace std;
int main() {
	set<int> st;//创建set
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		st.insert(x);//将每一个元素插入set中,自动排序去重
	}
	cout<<st.size()<<endl;//输出set的大小(即去重和排序后剩余的个数)
	for (set<int>::iterator it = st.begin(); it != st.end(); it++)//使用迭代器遍历set
		cout << *it << " ";//输出每个数
	cout << endl;
	return 0;
}

求一键三连~

原文