Files
python/TangDou/XiTi/1338.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

37 lines
972 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}