You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
972 B

2 years ago
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
//自定义排序函数
bool SortByValueKey(const pair<int, int> &v1, const pair<int, int> &v2)//注意本函数的参数的类型一定要与vector中元素的类型一致
{
if (v1.second == v2.second) return v1.first < v2.first;
return v1.second > v2.second;
}
int main() {
//录入数据到vector数组
int n;
cin >> n;
unordered_map<int, int> myMap;
for (int i = 0; i < n; i++) {
int c;
cin >> c;
myMap[c]++;
}
//保存到数组中
vector<pair<int, int>> vec;
for (auto iter = myMap.begin(); iter != myMap.end(); ++iter) {
vec.push_back(make_pair(iter->first, iter->second));
}
//开始自定义排序
sort(vec.begin(), vec.end(), SortByValueKey);
//输出结果
for (auto c : vec) {
cout << c.first << " " << c.second << endl;
}
return 0;
}