数据结构实验:哈希表
Time Limit: 1000MS Memory limit: 65536K
题目描述
在n个数中,找出出现次数最多那个数字,并且输出出现的次数。如果有多个结果,输出数字最小的那一个。
输入
单组数据,第一行数字n(1<=n<=100000)。
接下来有n个数字,每个数字不超过100000000
接下来有n个数字,每个数字不超过100000000
输出
出现次数最多的数字和次数。
示例输入
3 1 1 2
示例输出
1 2
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int cmp(int x,int y)
{
return x<y;
}
int main()
{
int n,a[100001],k=0,count=1,t;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
if(a[i]==a[i+1])
{
count ++;
}
else {if(count>k) k=count,t=a[i];count=1;}
}
printf("%d %d\n",t,k);
return 0;
}