1201: 众数问题

 

给定含有n个元素的多重集合S,每个元素在S中出现的次数称为该元素的重数。多重集S中重数最大的元素称为众数。
例如,S={1,2,2,2,3,5}。多重集S的众数是2,其重数为3。
编程任务:
对于给定的由n 个自然数组成的多重集S,编程计算S 的众数及其重数。
输入
第1行多重集S中元素个数n(n<=50000);接下来的n 行中,每行有一个自然数。
输出
输出文件有2 行,第1 行给出众数,第2 行是重数。(如果有多个众数,只输出最小的)
样例输入 Copy
6
1
2
2
2
3
5
样例输出 Copy
2
3
#include<bits/stdc++.h>
using namespace std;
bool cmp(const pair<int,int> a,const pair<int, int> b){
    return a.second < b.second;
}
int main()
{
    map<int,int>m;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int tem;
        cin>>tem;
        m[tem]++;
    }
    map<int,int >::iterator it1 = max_element(m.begin(),m.end(),cmp);
    cout<< it1->first << '\n' << it1->second <<endl;
    return 0;
}

 

 
posted @ 2019-03-22 22:00  Binary_tony  阅读(456)  评论(0编辑  收藏  举报