Huawei machine test - HJ3 explicit random number

describe

Mingming wants to invite some students to do a questionnaire survey in school. In order to make the experiment objective, He first generated N random integers between 1 and 1000 (n ≤ 1000) by computer. For the repeated numbers, only one is retained, and the rest of the same numbers are removed. Different numbers correspond to different student numbers. Then he sorted these numbers from small to large, and went to his classmates for investigation according to the arranged order. Please help Mingming complete the "de duplication" and "sorting" Work (there may be multiple groups of data in the same test case (for different surveys). I hope you can handle it correctly).

Note: the test case ensures the correctness of input parameters, and the respondents do not need to verify. There is more than one set of test cases.

When there is no new input, the input ends.

Data range: 1 < = n < = 1000, and the size of the entered number meets 1 < = ual < = 500

Enter Description:

Note: the input may have multiple sets of data (for different surveys). Each group of data includes multiple rows. In the first row, enter the number N of random integers, and then enter the corresponding number of integers in the next N rows. See the "example" below for the specific format.

Output Description:

Returns the processed result of multiple rows

Example 1

Input:

3
2
2
1
11
10
20
40
32
67
40
20
89
300
400
15

copy

Output:

1
2
10
15
20
32
40
67
89
300
400

copy

explain:

Example 1 contains two small examples!!  
Input explanation:
The first number is 3, that is, N=3 in this small example, which means that three random integers between 1 and 1000 are generated by computer. Next, there are three random numbers in total for each line, that is, the three random numbers are:
2
2
1
 Therefore, the output of the first small sample is:
1
2
 The first number of the second small example is 11, that is (similar to the above explanation)
So the output of the second small sample is:
10
15
20
32
40
67
89
300
400   

Solution 1 (de duplication, quick sorting, output):
#include<stdio.h>
#include<string.h>
 
void swap(int *a, int x, int y){
    int tmp=a[y];
    a[y]=a[x];
    a[x]=tmp;
}
 
void sort(int *a, int left, int right){
    if(left>=right){
        return ;   
    }
    int i=left;
    int j=right;
    int key=a[left];
     
    while(i<j){
        while(i<j && key<=a[j]){
            j--;
        }  
        swap(a,i,j);
        while(i<j && key>=a[i]){
            i++;
        }
        swap(a,i,j);
    }
    sort(a,left,i-1);
    sort(a,i+1,right);
}
 
int main(){
    int a[1001];
    int i,j,k,n;
    while(scanf("%d",&n)!=EOF){
        for (i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        for(i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
                if(a[i]==a[j]){
                    for(k=j;k<n;k++){
                        a[k]=a[k+1];
                    }
                    j--;
                    n--;
                }
            }
        }
        sort(a,0,n-1);
        for(i=0;i<n;i++){
            printf("%d\n",a[i]);
        }
    }
    return 0;
}

Solution II (fast scheduling of solution I) sort Function substitution):
#include<iostream>
#include<vector>
#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main(){
    int a[1001];
    int i,j,k,n;
    while(scanf("%d",&n)!=EOF){
        for (i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        for(i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
                if(a[i]==a[j]){
                    for(k=j;k<n;k++){
                        a[k]=a[k+1];
                    }
                    j--;
                    n--;
                }
            }
        }
        sort(a,a+n);
        for(i=0;i<n;i++){
            printf("%d\n",a[i]);
        }
    }
    return 0;
}

Solution III (utilization) set Container properties):
#include<iostream>
#include<vector>
#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main(){
	int i,n,m;
	while(cin>>n){
		set<int> a;//Using the set container can automatically realize the operation of de duplication and sorting, which is very important 
		for(i=0;i<n;i++){
			cin>>m;
			a.insert(m);//Insert into container a 
		}
		set<int>::iterator it;//set type iterator
		for(it=a.begin();it!=a.end();it++){
			cout<<*it<<endl;
		} 
	}
	return 0;
}

Solution 4 (using the relationship between serial number and assignment to form a mapping):
#include<iostream>
#include<vector>
#include<set>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main(){
    int a[1001];
    int i,k,n;
    while(scanf("%d",&n)!=EOF){
    	memset(a,0,sizeof(a));
        for (i=0;i<n;i++){
            scanf("%d",&k);
            a[k]=1;
        }
        for(i=0;i<1000;i++){
        	if(a[i]==1){
        		printf("%d\n",i);
			}
        }
    }
    return 0;
}

Added by voxanBoxer on Sun, 02 Jan 2022 09:22:31 +0200