CCF201503-2 数字排序

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值