[NOIP2006]明明的随机数

一、题目

登录---专业IT笔试面试备考平台_牛客网

二、代码

  • set去重,再利用vector进行排序
  • std::set是一个自带排序功能的容器,它已经按照一定的规则(默认是元素的小于比较)对元素进行了排序。因此,你不能直接对std::set使用std::sort,因为它已经有了自己的排序机制,而且不允许直接修改元素的值
cpp 复制代码
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    set<int>s;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        s.insert(x);
    }
    vector<int>v;
    set<int>::iterator it = s.begin();
    while (it != s.end())
    {
        v.push_back(*it);
        ++it;
    }
    sort(v.begin(), v.end());
    cout << s.size() << endl;
    it = s.begin();
    while (it != s.end())
    {
        cout << *it << " ";
        it++;
    }
    return 0;
}
相关推荐
郝学胜-神的一滴3 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
青少儿编程课堂20 小时前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
geovindu1 天前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗1 天前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
凌冰_1 天前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗1 天前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗1 天前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
lisw051 天前
稀疏算法概述!
算法·机器人·无人机
YLCHUP2 天前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划