NOIP 2006 obviously random number

Luo Gu P1059 obviously random number

Luo Gu Portal

JDOJ 1423: [NOIP2006] clearly random number T1

JDOJ Portal

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

Row 2 is input, the behavior of a first positive integer representing the number of the generated random number: N 2 line with a space-separated positive integer number N, the generated random number.

Output

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

Source

NOIP2006 popular group

answer:

Fundamentals of grammar questions: can simulate practice, practice can also set the container in the STL .

Code 1, pure analog approach:

#include<cstdio>//扫两遍,一遍出次数一遍输出
#include<algorithm>
using namespace std;
int n,ans;
int a[110];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        if(a[i]==a[i-1])
            continue;
        else
            ans++;
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++)
    {
        if(a[i]==a[i-1])
            continue;
        else
            printf("%d ",a[i]);
    }
    return 0;
}

FrontHigh Energy!!

There is a C ++ Party P Party never get over something: STL.

Exasperating reality is that when the party was still handwritten on P examination room when the fast row, C ++ people a sort (); you can too; P when the party was still writing code a simulation, C ++ a party set, code length and more small, concise, AC through.

So we have to learn this set container.

set is in English "collection" means, therefore, set itself a collection container.

But it have a new name: an ordered set.

Two features set us simply remember container: the element does not repeat the elements and orderly.

Is it not just to meet this title mean?

A slightly STL basis, we will be groping with setA out of this problem.

(Note: Because the STL container function almost the same, are a pass Belden)

Directly on the code:

#include<cstdio>
#include<set>
using namespace std;
int n;
set<int> s;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        s.insert(x);//集合插入函数
    }
    printf("%d\n",s.size());//集合大小函数
    while(!s.empty())//集合是否为空
    {
        printf("%d ",*s.begin());//注意这里一定要用*表示取值(begin是迭代器(返回指针))
        s.erase(s.begin());
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11388169.html