Hrbust Oj 1164 数字去重和排序 题解 (c ++ ) (set容器)

数字去重和排序
Time Limit: 5000 MS Memory Limit: 65536 K
Total Submit: 2389(440 users) Total Accepted: 543(350 users) Rating:  Special Judge: No
Description

用计算机随机生成了N个0到910305(包含0和910305)之间的随机整数(N≤100000000),对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再把这些数从小到大排序。
请你完成“去重”与“排序”的工作。

Input

输入有2行,第1行为1个正整数,表示所生成的随机数的个数:
N
第2行有N个用空格隔开的正整数,为所产生的随机数。

Output

输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。

Sample Input

10
20 40 32 67 40 20 89 300 400 15

Sample Output

8
15 20 32 40 67 89 300 400

Hint

推荐使用scanf  printf

输入数据有多组!

Author
黄李龙


之前的思路忘记了,WA*2,MLE*2,RE*2 .......

百度了下题解,c++ stl 容器中有 set 这样的容器,正好可以起到排序和去重的作用。代码如下:

#include <iostream>
#include <set>
using namespace std ;
int main(){
    set<int> container ;
    int n ;
    while ( cin >> n ){
        container.clear() ;
        for ( int i = 0 ; i < n ; i ++ ){
            int x ;
            cin >> x ;
            container.insert(x) ;
        }
        int num = container.size() ;
        set<int>::iterator ite ;
        cout << num << endl ;
        for ( ite = container.begin() ; ite != container.end() ; ite ++ ){
            if ( ite == container.begin() ) cout << *ite ;
            else cout << " " << *ite ;
        }
        cout << endl ;
    }
}

(记录下set以及迭代器的使用)

(日常膜拜dalao....)(o゜▽゜)o☆


猜你喜欢

转载自blog.csdn.net/colpac/article/details/79845283