问题描述
给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出。
输入格式
输入的第一行包含一个整数n,表示给定数字的个数。
第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。
第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。
输出格式
输出多行,每行包含两个整数,分别表示一个给定的整数和它出现的次数。按出现次数递减的顺序输出。如果两个整数出现的次数一样多,则先输出值较小的,然后输出值较大的。
样例输入
12
5 2 3 3 1 3 4 2 5 2 3 5
5 2 3 3 1 3 4 2 5 2 3 5
样例输出
3 4
2 3
5 3
1 1
4 1
2 3
5 3
1 1
4 1
评测用例规模与约定
1 ≤ n ≤ 1000,给出的数都是不超过1000的非负整数。
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int ,int> P;
//比较两个pair类型的大小
bool cmp(const P &a, const P &b)
{
return a.second > b.second; //从大到小排序
}
int main()
{
int n;
cin >> n;
map<int, int> m;
vector<P> v; //存放pair类型的vector,调用sort函数
for(int i = 0;i<n;i++)
{
int x;
cin >> x;
m[x] ++; //用下标存入
}
for(map<int,int>::iterator it=m.begin();it!=m.end();++it)
{
//v.push_back(make_pair(it->first,it->second));
v.push_back(*it);
}
sort(v.begin(),v.end(),cmp); //排序
for(vector<P>::iterator it=v.begin();it!=v.end();++it)
{
cout << it->first <<' '<< it->second <<endl;
}
//getchar();
//getchar();
return 0;
}