[P1059] Obviously random number (sort + unique)

topic

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, 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 and output formats

Input format:
inputted row 2, line 1 is a positive integer representing the number of the generated random numbers:

2 has N rows separated by a space positive integer number N, the generated random number.

Output format:
output row is 2, 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 and output

Input Sample # 1: copy
10
20 is 40 3,267,402,089,300,400 15
outputs Sample # 1: Copy
. 8
15 32 40 20 is 67 89 300 400
Description

NOIP 2006 universal set of the first question

Ideas:

The STL
Sort + UNIQUE
interval operation is generally closed left-right open [,)
UNIQUE deduplication complexity of O (n) can only be removed continuously repeated element
return value is a pointer, after a last one of the sequence to re-pointing;
repeat elements will be put back
to zero need not keep the number of unique words -1 -1 1 start memory;

#include<bits/stdc++.h>
using namespace std;
int n,a[1001];
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i) cin>>a[i];
    sort(a+1,a+n+1);
    int m=unique(a+1,a+n+1)-a-1;
    cout<<m<<endl;
    for(int i=1;i<=m;++i)
        cout<<a[i]<<' ';
    return 0;
}
发布了75 篇原创文章 · 获赞 80 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_36693514/article/details/78464839