RQNOJ Obviously random numbers

Topic link:
Portal

Topic description:
Obviously I want to invite some students to do a questionnaire survey in school. For the objectivity of the experiment, he first generated N random integers between 1 and 1000 (N≤100) with a computer. Keep only one number, and remove the rest of the same number. Different numbers correspond to different student IDs. Then sort these numbers from smallest to largest, and go to classmates to do the survey in the sorted order. Please help clearly complete the work of "de-duplication" and "sorting".

Input format:
There are 2 lines of input, the first line is 1 positive integer, indicating the number of random numbers generated:
N
The second line has N positive integers separated by spaces, which are the random numbers generated.

Output format: The
output is also 2 lines, the first line is a positive integer M, which represents the number of different random numbers. The second line is M positive integers separated by spaces, which are different random numbers sorted from small to large.

Sample input:
10
20 40 32 67 40 20 89 300 400 15

Sample output:
8
15 20 32 40 67 89 300 400

c language reference code:

#include <stdio.h>
#include <stdlib.h>

#define MAX 1001

int a[MAX];
int visit[MAX];

int cmp(const void *a,const void *b)
{
    
    
	int* pa=(int*)a;
	int* pb=(int*)b;
	int num1=*pa;
	int num2=*pb;
	int t=num1-num2;
	return t;
}

int main()
{
    
    
	int N,i=1,j=0,x,count=0;
	scanf("%d",&N);
	
    while(i<=N)
    {
    
    
    	scanf("%d",&x);
    	if(visit[x]==0)
    	{
    
    
    		visit[x]=1;
    		a[j++]=x;
    		count++;
		}
		i++;
	}
	
	qsort(a,count,sizeof(a[0]),cmp);
    
	printf("%d\n",count);
    for(j=0;j<count;j++)
     printf("%d ",a[j]);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/115125632