華爲機試題--3.明明的隨機數

題目描述:
明明想在學校中請一些同學一起做一項問卷調查,爲了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應着不同的學生的學號。然後再把這些數從小到大排序,按照排好的順序去找同學做調查。請你協助明明完成“去重”與“排序”的工作。
Input Param
n 輸入隨機數的個數
inputArray n個隨機整數組成的數組

Return Value
OutputArray 輸出處理後的隨機整數

輸入描述:輸入多行,先輸入隨機整數的個數,再輸入相應個數的整數

輸出描述:返回多行,處理後的結果

輸入例子:
11
10
20
40
32
67
40
20
89
300
400
15

輸出例子:
10
15
20
32
40
67
89
300
400

去重,排序:
sort() unique() earse()

#include <iostream>
#include <random>
#include <algorithm>
using namespace std;
void Solution(int num);
int main()
{
    int count;
    while(cin >> count)
        Solution(count);
    return 0;
}
void Solution(int num)
{
    vector<int> rnd;
    int opt;
    for (int i = 0; i < num; ++i)
    {
        cin >> opt;
        rnd.push_back(opt);
    }
    sort(rnd.begin(),rnd.end());
    auto end = unique(rnd.begin(), rnd.end());
    rnd.erase(end,rnd.end());
    for (int i : rnd)
        cout << i << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章