本文主要是介绍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++题解]:哈希表、排序、结构体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!