STL map

问题 B: 【枚举】众数

题目描述

给出N个l到30000之间的无序正整数,其中1≤N≤10000,同一个正整数可能会出现多次,出现次数最多的整数称为众数。求出它的众数及它出现的次数。

 

输入

第1行是正整数的个数N,第2行开始为N个正整数。

 

输出

若干行,每行两个数,第1个是众数,第2个是众数出现的次数。

 

样例输入

12
2 4 2 3 2 5 3 7 2 3 4 3

 

样例输出

2 4
3 4
#include<iostream>
#include<cmath>
#include<map>
#include<algorithm>

using namespace std;

int main()
{
    int n;
    cin>>n;
    map<int,int> a;
    int ans = 0;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        a[x]++;
        ans = max(ans,a[x]);
    }
    map<int,int>::iterator it;
    for(it=a.begin();it!=a.end();it++)
    {
        if(it->second==ans)
            cout<<it->first<<" "<<it->second<<endl;
    }
}

 

 
posted @ 2018-07-20 16:35  zangzang  阅读(152)  评论(0编辑  收藏  举报