数据结构实验:哈希表
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
在n个数中,找出出现次数最多那个数字,并且输出出现的次数。如果有多个结果,输出数字最小的那一个。
Input
单组数据,第一行数字n(1<=n<=100000)。
接下来有n个数字,每个数字不超过100000000
Output
出现次数最多的数字和次数。
Sample Input
3 1 1 2
Sample Output
1 2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100001];
int cnt[100001];
int main()
{
int n, m;
scanf("%d", &n);
for(int i = 0;i < n; i++)
{
int x;
scanf("%d", &a[i]);
}
sort(a, a+n);
int tmp=1;
int maxx = 0;
int pos=0;
for(int i = 1; i < n; i++)
{
if(a[i] == a[i-1])
{
cnt[tmp]++;
if(maxx < cnt[tmp])
{
maxx = cnt[tmp];
pos = tmp;
}
}
else
{
tmp = i;
}
}
printf("%d %d\n",a[pos],cnt[pos]+1);
return 0;
}