题目![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5f2b26e9817772fb18f0277846cfcf02.png)
思路
这是一道统计排序题目,此处使用map进行统计,利用优先队列priority_queue
进行排序,后进行输出
代码
#include <iostream>
#include <map>
#include <queue>
using namespace std;
struct node {
int key, count;
bool operator < (const node &n) const {
if (count == n.count)
return key > n.key;
else
return count < n.count;
}
};
int main() {
priority_queue<node> q;
map<int, int> m;
int n, v;
// 输入数据,构建Map
cin >> n;
for (int i = 0; i < n; i++) {
cin >> v;
m[v]++;
}
// 构建优先队列进行排序
node keyval;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
keyval.key = it->first;
keyval.count = it->second;
q.push(keyval);
}
// 输出结果
while (!q.empty()) {
keyval = q.top();
q.pop();
cout << keyval.key << " " << keyval.count << endl;
}
return 0;
}