数字排序
from CCF-CSP 2015-03-2
Time limit:1s
Memory limit:256MB
ac代码:
#include<bits/stdc++.h>
using namespace std;
int n,m;
pair<int,int> p[1005]; //first存储数字,second存储该数字出现的次数
bool cmp(pair<int,int> p1,pair<int,int> p2){ //排序规则是按照出现次数多的排序
if(p1.second == p2.second) //次数相同,按数字小排序
return p1.first < p2.first;
return p1.second > p2.second;
}
int main(){
cin>>n;
for(int i = 0;i <= 1000;++i) //初始化
p[i].first = i,p[i].second = 0;
while(n--)
cin>>m,++p[m].second;
sort(p,p + 1001,cmp);
for(int i = 0;p[i].second != 0;++i)
cout<<p[i].first<<" "<<p[i].second<<"\n";
return 0;
}