稀碎,先写着吧,以后常看常改
题目来源于洛谷:https://www.luogu.com.cn/problem/P1059
以下是题目:
这题可以用一个set容器来实现,那么set容器是什么呢?
下面让我娓娓道来,对于菜鸡的我,只需要知道set容器会将存入的数自动按升序排好,并且能把容器中已有的数自动去重(已有的事,后必不再有 【狗头】),也就是说,容器里面的数只能出现1次。
那么用set容器来做题就很适合了!
【手动分割线】
话不多说,上代码:
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
int main()
{
int n;
//用set容器存这些数
set<int> s;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int num;
scanf("%d", &num);
s.insert(num);
}
//set容器的成员函数,size()计算容器的大小(也就是有几个数)
printf("%d\n", s.size());
//set容器不支持随机访问,所以要用迭代器对里面的数据进行访问
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
printf("%d ", *it);
}
return 0;
}
好的,那我现在溜了🙂