二分法 哈希表送上(详见sdut1480)

来源:互联网 发布:淘宝网店装修多少钱 编辑:程序博客网 时间:2024/06/07 12:48

数据结构实验:哈希表

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

 在n个数中,找出出现次数最多那个数字,并且输出出现的次数。如果有多个结果,输出数字最小的那一个。

输入

 单组数据,第一行数字n(1<=n<=100000)。
接下来有n个数字,每个数字不超过100000000

输出

 出现次数最多的数字和次数。

示例输入

31 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=count+1;          }          else  {if(count>k) k=count,t=a[i];count=1;}      }      printf("%d %d\n",t,k);      return 0;  }  



0 0