zoukankan      html  css  js  c++  java
  • 15年3月CCF真题2-数字排序

    问题描述

      给定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和multimap的自动排序,都帮你直接搞定了

     1 #include <utility>
     2 #include <iostream>
     3 #include <map>
     4 #include <iterator>
     5 #include <functional>
     6 
     7 using namespace std;
     8 
     9 int main(){
    10     map<int,int> j;
    11     multimap<int,int,greater<int> > jj;
    12     int n;
    13     cin>>n;
    14     for(int i=0;i<n;i++)
    15     {
    16         int a;
    17         cin>>a;
    18         j[a]++;
    19     }
    20     for(map<int,int>::iterator it = j.begin();it!=j.end();it++)
    21     {
    22         jj.insert(pair<int,int>(it->second,it->first));
    23     }
    24     for(map<int,int>::iterator it = jj.begin();it!=jj.end();it++)
    25     {
    26         cout<<it->second<<" "<<it->first<<endl;
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    陈欧代言
    location传值
    jsp中button传值
    电影
    排序
    比较两个字符,相等输出yes,不相等输出no
    查表求平方值
    数据库查询调优(SQL 2008)
    HelloWorld
    关于缓存 (这个自己也在慢慢学习,慢慢总结中,有路过的,求指点,赶紧不尽。。。)
  • 原文地址:https://www.cnblogs.com/Outer-Haven/p/4699129.html
Copyright © 2011-2022 走看看