本题考查数组的一些操作。首先,对于删去重复数字的操作,利用哈希表来存储每一个数字。如果某一个数字已经在哈希表中,则忽略该数字。取出筛选后的哈希表中的数字,并进行排序,最后输出。缺点是耗时有点多。
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
unordered_map<int, int> hash_map;
for (int n = 0; n < N; n++) {
int k;
cin >> k;
if (hash_map.count(k) == 0) {
// 元素k不存在,则放入哈希表中
hash_map.insert({k, 1});
}
}
int count = hash_map.size();
vector<int> arr;
for (auto &map : hash_map) {
arr.push_back(map.first);
}
sort(arr.begin(), arr.end());
for (vector<int>::iterator iter = arr.begin(); iter != arr.end(); iter++) {
cout << *iter << endl;
}
return 0;
}