数据结构实验:哈希表

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1480

#include <stdio.h>
#include <string.h>
int n;
#define N 100001  //N的值取比哈希表长度略大的(质数)
int a[100001],b[100001];
int main()
{
    int i,j,ad,t;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(i=0;i<n;i++)
    {
      scanf("%d",&t);
      ad=t%N;
      if(ad<0) ad=ad+N;
      while(1)
      {
         if(a[ad]==0||a[ad]==t)
         {
            a[ad]=t;
            b[ad]++;
            break;
         }
         else
         {
            ad++;
            if(ad>=N) ad=0;
         }
      }
    }
      int max=b[0];
      t=0;
      for(i=1;i<N;i++)
      {
          if(max<b[i]||(max==b[i]&&a[t]>a[i]))
          {
            max=b[i];
            t=i;
          }
      }
      printf("%d %d\n",a[t],max);



    return 0;
}
 

 

posted @ 2014-06-30 11:04  人艰不拆_zmc  阅读(413)  评论(0编辑  收藏  举报