哈希表

数据结构实验:
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

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

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

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

3
1 1 2
Sample Output

1 2
Hint

Source

cz

#include <stdio.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f
struct node
{
    int data;
    struct node *next;
}*h[100001];//链表后连接取余相同的点
struct node *newnode()
{
    struct node *t;
    t=(struct node *)malloc(sizeof(struct node));
    t->next=NULL;
    return t;
};
void in(int x)
{
    struct node *p;
    p=newnode();
    int z=x%100000;
    p->data=x;
    p->next=h[z]->next;
    h[z]->next=p;
}
int find(int x)
{
    struct node *p;
    int count=0;
    int z=x%100000;
    p=h[z]->next;  //在对应的链表头后面记等于x的个数
    while(p)
    {
        if(p->data==x)
        {
            count++;
        }
        p=p->next;
    }
    return count;
}
int main()
{
    int n,i,max,maxx,a[100001];
    scanf("%d",&n);
    for(i=0;i<=100000;i++)
    {
        h[i]=newnode();
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        in(a[i]);
    }max=-1;maxx=INF;
    for(i=0;i<n;i++)
    {                                 //如果有多个结果是最大值,输出数字最小的那一个
        if((find(a[i])>max)||(find(a[i])==max&&maxx>a[i]))  //maxx记住数字最小的那一个
        {
            max=find(a[i]);
            maxx=a[i];
        }
    }
    printf("%d %d\n",maxx,max);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值