#include <iostream>
#include<algorithm>
#include<map>
using namespace std;
const int N = 1001;
struct Num{
int val;
int cnt;
bool operator<(const Num &a )const{
//重点1,重载<运算符,是在排谁在前。
//如这个题,要求的是按照出现次数排序,如果次数相同则按照值从小到大排序。
return cnt == a.cnt?val < a.val:cnt > a.cnt;
}
}a[N];
int main()
{
int n,m,i,j,k;
map<int,int> d;//重点2,map<>声明及定义。c++和c语言声明和定义都是一个风格,
//而java则是声明和定义分开的,另一种风格。
cin >> n;
while(n--){
cin >> k;
d[k]++;
}
i = 0;
for(map<int,int> ::iterator it = d.begin();it != d.end();it++){
//重点3,迭代器的用法。注意这里i=0 不能写在for的初始化里面,会报错。
a[i].val = it->first;
//重点4,map的key和value是用的first 和second的
a[i].cnt = it->second;
++i;
}
sort(a,a+i);
for(k = 0;k < i;k++)
cout << a[k].val <<" " << a[k].cnt <<endl;
return 0;
}
CCF201503-2 数字排序
最新推荐文章于 2020-05-11 14:42:07 发布