CCF 201312-1 出现次数最多的数 C++语言实现

#include<iostream>
#include<map>
using namespace std;

int main()
{
    // 读入一共有多少个数据 -> n
    int n;
    cin>>n;
    // 使用 map容器 来管理
    map<int,int> store;
    int input;
    for(int k=0;k<n;k++)
    {
        cin>>input;
        // map容器 "key值" 表示出现的正整数 
        //  map容器 "value" 表示该正整数出现的个数
        if(store.count(input)>0) store[input]++; //如果已经出现过
        else store.insert( std::pair<int,int>(input,1) ); //正整数没有出现过
    }
    int max = 0; // 代表value
    int index = 0; // 代表索引,目标是寻找索引最小的数
    for(map<int,int>::iterator it=store.begin();it!=store.end();it++)
    {
        if(it->second==max) index = min(index,it->first);
        if(it->second>max)
        {
            max = it->second;
            index = it->first;
        }
    }
    cout<<index<<endl;
    return 0;
}

这里需要提醒的是,容器的迭代器至少在官网上还是需要老老实实按照map<int,int>::iterator去写,不能使用C++11的auto关键字

如果使用的话,在Dev-c++中是无法通过编译,所以在平台上也是直接报编译错误,0分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值