华为机试题2

数组去重并排序

思路:先去重后排序或者先排序后去重

可以使用STL来求,set内部是有序的,list内部不是有序的。

样例:

输入:

4

6 3 3 9

输入

3 6 9

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <set>
 4 #include <list>
 5 using namespace std;
 6 
 7 void RemoveRep(int arr[],int N)
 8 {
 9     set<int> s;
10     pair< set<int>::iterator, bool > m;
11     list<int> list1;
12 
13     for(int i=0;i<N;i++){
14         m=s.insert(arr[i]);
15         if(m.second) list1.push_back(arr[i]);
16 
17     }
18     for(set<int>::iterator it=s.begin();it!=s.end(); it++)
19         cout<<*it<<" ";
20 }
21 
22 int main()
23 {
24     int a[4] = {6,3,3,9};
25     RemoveRep(a,4);
26     return 0;
27 }
View Code

 

posted @ 2015-09-20 18:34  天天AC  阅读(271)  评论(0编辑  收藏  举报