zoukankan      html  css  js  c++  java
  • CCF CSP 201503-2 数字排序 (map+自定义排序)

    题目链接:http://118.190.20.162/view.page?gpid=T26

    问题描述
    试题编号: 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的非负整数。

    先统计每个数出现的次数,然后存入map,本来以为可以直接用map按照值的大小排序,但是好像自带的multimap只能是按照键大小排序,所以我先按照键的大小排序,然后再存入自定义的结构体中自定义排序即按照值的大小进行排序:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath> 
     4 #include <string>
     5 #include <cstring>
     6 #include <map> 
     7 using namespace std;
     8 int n,x;
     9 struct node
    10 {
    11     int x,y;
    12 }a[1005];
    13 bool cmp(node m,node n)
    14 {
    15     
    16     if(m.y==n.y) return m.x<n.x;
    17     return m.y>n.y;
    18 }
    19 int main()
    20 {
    21     while(cin>>n){
    22         multimap<int,int,greater<int> >mp;//按照键大小排序 
    23         map<int,int> ::iterator it;
    24         mp.clear();
    25         for(int i=0;i<n;i++){
    26             cin>>x;
    27             it=mp.find(x);
    28             if(it==mp.end()) mp.insert({x,1});
    29             else it->second++;
    30         }
    31         int t=0;
    32         for(it=mp.begin();it!=mp.end();it++){
    33             a[t].x=it->first;
    34             a[t].y=it->second;
    35             t++;
    36             //cout<<it->first<<" "<<it->second<<endl;
    37         } 
    38         //cout<<"================"<<endl;
    39         sort(a,a+t,cmp);
    40         for(int i=0;i<t;i++){
    41             cout<<a[i].x<<" "<<a[i].y<<endl;
    42         }
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    苹果一体机发射Wi-Fi
    iphone 屏蔽系统自动更新,消除设置上的小红点
    data parameter is nil 异常处理
    copy与mutableCopy的区别总结
    java axis2 webservice
    mysql 远程 ip访问
    mysql 存储过程小问题
    mysql游标错误
    is not writable or has an invalid setter method错误的解决
    Struts2中关于"There is no Action mapped for namespace / and action name"的总结
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10731203.html
Copyright © 2011-2022 走看看