CCF 201503-2 数字排序

试题编号: 201503-2
试题名称: 数字排序
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出。
输入格式
  输入的第一行包含一个整数n,表示给定数字的个数。
  第二行包含n个整数,相邻的整数之间用一个空格分隔,表示所给定的整数。
输出格式
  输出多行,每行包含两个整数,分别表示一个给定的整数和它出现的次数。按出现次数递减的顺序输出。如果两个整数出现的次数一样多,则先输出值较小的,然后输出值较大的。
样例输入
12
5 2 3 3 1 3 4 2 5 2 3 5
样例输出
3 4
2 3
5 3
1 1
4 1
评测用例规模与约定
  1 ≤ n ≤ 1000,给出的数都是不超过1000的非负整数。

关键词:尾遍历

 1 #include<iostream>
 2 #include<map>
 3 #include<set>
 4 using namespace std;
 5 int main(){
 6     //freopen("in2.txt","r",stdin);
 7     int gn;
 8     map<int,int> m;
 9     cin >> gn;
10     for(int i = 0;i<gn;i++){
11         int buf;
12         cin >> buf;
13         if(m.find(buf) != m.end()){
14             m[buf]++;
15         }
16         else{
17             m[buf] = 1;
18         }
19     }
20     set<int> s;
21     for(map<int,int>::iterator it = m.begin();it!=m.end();it++){
22         if(s.find(it->second) == s.end()){
23             s.insert(it->second);
24         }
25     }
26     while(!s.empty()){
27         set<int>::iterator its = s.end();
28         its--;
29         for(map<int,int>::iterator it = m.begin();it!=m.end();it++){
30             if(it->second == *its){
31                 cout << it->first << ' ' << it->second << endl;
32             }
33         }
34         s.erase(its);
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/ywsswy/p/7739161.html