2015032-数字排序
问题描述
| 试题编号: | 201503-2 |
| 试题名称: | 数字排序 |
| 时间限制: | 1.0s |
| 内存限制: | 256.0MB |
| 问题描述: | 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出。 输入格式 输入的第一行包含一个整数n,表示给定数字的个数。 输出格式 输出多行,每行包含两个整数,分别表示一个给定的整数和它出现的次数。按出现次数递减的顺序输出。如果两个整数出现的次数一样多,则先输出值较小的,然后输出值较大的。 样例输入 12 样例输出 3 4 评测用例规模与约定 1 ≤ n ≤ 1000,给出的数都是不超过1000的非负整数。 |
程序代码:
#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;int main()
{int n,num;cin>>n;unordered_map<int,int> m;for(int i=0;i<n;i++){cin>>num;++m[num];}vector<pair<int,int>> q;for(auto i:m)q.push_back(i);sort(q.begin(),q.end(),[](const pair<int,int> &a, const pair<int,int> &b){if(a.second == b.second) return a.first < b.first;return a.second > b.second;});for(auto &i:q)cout<<i.first<<' '<<i.second<<endl;return 0; }