ZeroJudge B130: Ming-Ming's Random Number

Mingming wants to conduct a survey at school with some classmates. To ensure the objectivity of the experiment, he first uses a computer to generate N random integers between 1 and 1000 (N ≤ 100). For any duplicate numbers, he keeps only one and removes the rest. Each unique number corresponds to a different student's student ID. Then, he sorts these numbers in ascending order and invites classmates to the survey according to the sorted order. Can you help Mingming complete the tasks of "removing duplicates" and "sorting?"

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
Each input consists of 2 lines. The first line contains a positive integer indicating the number of randomly generated numbers: N.
The second line contains N positive integers separated by spaces, representing the randomly generated numbers.
The output for each group is also 2 lines. The first line contains a positive integer M, indicating the number of distinct random numbers. The second line contains M positive integers separated by spaces, representing the sorted distinct random numbers in ascending order.
10
20 40 32 67 40 20 89 300 400 15
8
15 20 3 2 40 6 7 89 300 400

Thought Process

You can use a **Map** to determine if a number has appeared before. If it has appeared, don't add it to the array/vector. If it hasn't appeared, add it to the array and increase the corresponding value in the map by 1. Finally, output the size of the array and the sorted array.

Sample Code-ZeroJudge B130: Ming-Ming's Random Number

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    vector<int>num;
    map<int, int>MAP;
    for (int i = 0; i<N; i++)
    {
        int tmp;
        cin >> tmp;
        if (MAP[tmp] == 0) num.push_back(tmp);
        MAP[tmp]++;
    }
    sort(num.begin(), num.end());
    int size = int(num.size());
    cout << size << "\n";
    for (int i = 0; i<size; i++)
    {
        cout << num[i] << " ";
    }
    cout << "\n";
}

//ZeroJudge B130
//Dr. SeanXD

Comments