数据结构实验:哈希表(SDUT 1480)

来源:互联网 发布:软件项目管理实践 编辑:程序博客网 时间:2024/04/30 06:07

数据结构实验:哈希表

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

题目描述

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

输入

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

输出

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

示例输入

31 1 2

示例输出

1 2

提示

 

来源

 cz

示例程序

 


 哈希法的冲突运算。先挂后找~

#include <stdio.h>#include <stdlib.h>#define N 100000int a[100000];struct node{    int date;    struct node *next;}*h[N];void gua (int x){    int a=x%N;    struct node *p;    p=(struct node *)malloc(sizeof(struct node ));    p->date=x;    p->next=h[a];    h[a]=p;}int findx (int x){    int a = x%N;    int count  = 0;    for (struct node *p = h[a]; p; p = p->next)    {        if (p->date == x)             count ++;    }    return count ;}int main(){    int n, i, x, flag, max=0;    scanf("%d",&n);    for(i=0;i<n;i++)    {        scanf("%d",&a[i]);        gua (a[i]);    }    for(i=0;i<n;i++)    {        if(max<findx (a[i]))        {            max=findx (a[i]);            flag =i;        }    }    printf("%d %d\n",a[flag],max);    return 0;}


0 0
原创粉丝点击