#P2135. 众数.填空题(NHOI2015年初中组t2)
众数.填空题(NHOI2015年初中组t2)
题目描述
小明最近在上数学课,老师给小明布置了个作业:在 n 个数里找出所有的众数。
众数的定义是这样的:在所有数当中出现次数最多的数被称为众数。并且根据定义,众数有可能有多个。你能解决这个问题吗?
输入格式
第 1 行一个整数 n。
第 2 行有 n 个整数, 表示第 i 个数。
数据规模
40% 的数据,1<=n<=400
100% 的数据,1<=n<=1000000,1 <= <= 1000000000
输出格式
输出第 1 行,包括一个整数 k,表示众数的个数。
接下来一行包括 k 个整数,每个整数都表示一个众数,并且从小到大输出。
样例
10
3 3 3 2 3 1 2 2 1 2
2
2 3
完成程序
#include<bits/stdc++.h>
using namespace std;
map <int,int> m;
int n,MAX,cnt,a[100001];
int main(){
cin>>n;
int t;
for(int i=1;i<=n;i++)
{
cin>>t;
__填空(1)__ ;
}
// 迭代器
map<int,int>::iterator it;
for( it= __填空(2)__ ; __填空(3)__ ;it++){
if(it->second>MAX){
MAX = __填空(4)__;
a[1] = it-> first;
cnt = 1;
}else if(it->second == MAX){
__填空(5)__
}
}
printf("%d\n",cnt);
for(int i=1;__填空(6)__;i++)
printf("%d ",a[i]);
return 0;
}
填空(1):{{ input(1) }}
填空(2):{{ input(2) }}
填空(3):{{ input(3) }}
填空(4):{{ input(4) }}
填空(5):{{ input(5) }}
填空(6):{{ input(6) }}