给定 n 个正整数,找出它们中出现次数最多的数。
如果这样的数有多个,请输出其中最小的一个。
输入格式
输入的第一行只有一个正整数 n,表示数字的个数。
输入的第二行有 n 个整数 s1,s2,…,sn。
相邻的数用空格分隔。
输出格式
输出这 n 个次数中出现次数最多的数。
如果这样的数有多个,输出其中最小的一个。
数据范围
1 ≤ n ≤ 1000
1 ≤ si ≤ 10000
输入样例:
6
10 1 10 20 30 20
输出样例:
10
AC代码:
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10005;
int a[N];
int main()
{
int n,x,max;
while (cin >> n)
{
while (n--)
{
cin >> x;
a[x]++;//a数组的元素代表其对应下标出现的次数
}
max = 0;
for (int i = 0;i < N;i++)
if (a[i] > max)
max = a[i];//找到出现次数最多且最大的元素的下标
for (int j = 0;j < N;j++)
{
if (a[j] == max)
{
cout << j << endl;
break;//找到出现次数最多且最小的元素
}
}
}
return 0;
}