您的位置:首页 > 其它

华为机试题2

2015-09-20 18:34 239 查看
数组去重并排序

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

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

样例:

输入:

4

6 3 3 9

输入

3 6 9

#include <iostream>
#include <algorithm>
#include <set>
#include <list>
using namespace std;

void RemoveRep(int arr[],int N)
{
set<int> s;
pair< set<int>::iterator, bool > m;
list<int> list1;

for(int i=0;i<N;i++){
m=s.insert(arr[i]);
if(m.second) list1.push_back(arr[i]);

}
for(set<int>::iterator it=s.begin();it!=s.end(); it++)
cout<<*it<<" ";
}

int main()
{
int a[4] = {6,3,3,9};
RemoveRep(a,4);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: