Random number algorithm obviously the title of ACM (C ++ language using bucket sort solve the problem)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_36940806/article/details/88599232

One, Title Description

   Obviously I want to invite some students in the school together to do a survey, in order to test the objectivity, his first with the computer-generated random integer (N≤100) between 1 and 1000 N number, for which duplicate numbers, only a reservation, to remove the remaining the same number, corresponding to the number of different students learn different number. Then put these numbers in ascending order, to get the students to do research in accordance with good row order. Please help obviously complete "de-duplication" and "sort" work.

Input Format

Row 2 is input, the behavior of a first positive integer representing the number of the generated random numbers:

N

The second row has space-separated positive integer number N, the generated random number.

Output Format

2 is an output line, line 1 is a positive integer M, represents the number of different random numbers. The second row are separated by spaces M a positive integer, from small to large are not sorted same random number.

Sample input

10
20 40 32 67 40 20 89 300 400 15

Sample Output

8
15 20 32 40 67 89 300 400

Second, problem-solving ideas

The number of the distinct number of input numbers: Variable Description 1: N: the number required to enter a [1001]:. Array declared initial value assigned 0. curNum: the number of the current input flag

2. a size of 1001 opened up array and assign a value of 0 at  

3. receiving N number of input, each receiving curNum, determines subscripts corresponding to the number of the input array element value is 0, and if yes, flag ++, and the array subscript of the array element number of input values ​​to input curNum number, i.e., a [curNum] = curNum;

4. The number of distinct output of the number of flag

5. a array traversal, if non-zero element of the array, the output

Third, the code shows

#include<iostream>
using namespace std;
int main()
{
    int N;
    int curNum;
    int flag = 0;
    int a[10001]= {0};
    cin>>N;
    for(int i =0; i<N; i++) {
        cin>>curNum;
        if(a[curNum] == 0)
            flag++;
        a[curNum] = curNum;
    }
    cout<<flag<<endl;
    for(int i =1;i<10001;i++){
        if(a[i] !=0)
            cout<<a[i]<<" ";
    }
}

 

Guess you like

Origin blog.csdn.net/qq_36940806/article/details/88599232
Recommended