SDUTOJ 1480 -- 哈希表(链地址法)

来源:互联网 发布:新还珠格格第三部知画 编辑:程序博客网 时间:2024/06/01 08:49

题目描述

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

输入

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

输出

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

示例输入

31 1 2

示例输出

1 2


#include <stdio.h>#include <string.h>#include <stdlib.h>const int MAX = 100000;struct node{    int num;    int time;    node *next;}st[100050];int main(){    int n,i;    memset(st,0,sizeof(st));    node *p,*tp;    scanf("%d",&n);    while(n--)    {        long long shu;        int tmp;        scanf("%lld",&shu);        tmp=shu%MAX;        p=&st[tmp];        tp=p;        while(p!=NULL)        {            if(p->num==shu)            {                p->time++;                break;            }            tp=p;            p=p->next;        }        if(p==NULL)        {            p=new node;            p->num=shu;            p->time=1;            p->next=NULL;            tp->next=p;        }    }    int a_num;    int a_time=0;    for(i=0;i<MAX;i++)    {        p=st[i].next;        while(p!=NULL)        {            if(a_time<p->time)            {                a_time=p->time;                a_num=p->num;            }            else if(a_time==p->time)            {                a_num=a_num<p->num?a_num:p->num;            }            p=p->next;        }    }    printf("%d %d\n",a_num,a_time);    return 0;}


0 0
原创粉丝点击