[] Clearly re-ordering to a random number

Title Description
obviously 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 , but one, the rest the same number removed, different numbers corresponding to different students learn numbers. 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
has two lines, line 1 is a positive integer N, the number of random numbers generated.
The second row has space-separated positive integer number N, the generated random number.
Output
2 lines, line 1 is a positive integer M, the same random number does not represent the number. The second row are separated by spaces M a positive integer, from small to large are not sorted same random number.
Sample input the Copy
10
20 is 40 32 67 20 is 40 89 300 400 15
sample output from the Copy
. 8
15 32 40 20 is 67 89 300 400

AC Code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
/*解题的关键是 排序和去重 */

bool p[1000+5];        /*(bool)去重,标记每个进入数组的元素*/
int main()
{
    int n,j=0,k,i,l,a[1000+5],cnt=0;
    scanf("%d",&n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&k);
        if(p[k])continue;   /*如果是新进入不重复的元素x,则p[x]置为true,同时x可进入a数组,
                            当下次遇到时,则因为p[x]为true不能进入数组a*/
        p[k]=1;
        a[j++]=k;          /*需要单独开计数变量j*/

    }
    sort(a,a+j);       /*排序*/
    printf("%d\n",j);
    for(i=0;i<j;i++)
    {

            printf("%d",a[i]);
            if(i!=j-1)printf(" ");


    }

    return 0;

}

Released two original articles · won praise 0 · Views 57

Guess you like

Origin blog.csdn.net/weixin_45800034/article/details/104081180