欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

CSP认证201503-2数字排序[C++题解]:哈希表、排序、结构体

发布时间:2025/4/5 c/c++ 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CSP认证201503-2数字排序[C++题解]:哈希表、排序、结构体 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目分析


来源:acwing

分析: 统计出现次数,用哈希表(这里用数组模拟一下),然后用结构体数组来存,而且方便排序。

ac代码

#include<bits/stdc++.h> using namespace std; const int N = 1010;struct Number{int id, cnt;bool operator<(const Number& t)const{if(cnt != t.cnt) return cnt > t.cnt;return id < t.id;} }d[N];int n; int a[N];int main(){cin >> n;for(int i = 0; i< n; i ++){int x;cin >> x;a[x] ++;}int idx = 0;for(int i = 0; i <= 1000; i++)if(a[i])d[idx ++] = {i,a[i]};sort(d, d + idx);for(int i = 0; i< idx; i ++){cout << d[i].id <<" " << d[i].cnt << endl;} }

题目来源

https://www.acwing.com/problem/content/3216/

总结

以上是生活随笔为你收集整理的CSP认证201503-2数字排序[C++题解]:哈希表、排序、结构体的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。