数据结构实验:哈希表

来源:互联网 发布:java action定义 编辑:程序博客网 时间:2024/06/07 22:44

点击获取原题链接

数据结构实验:哈希表
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
在n个数中,找出出现次数最多那个数字,并且输出出现的次数。如果有多个结果,输出数字最小的那一个。
Input
单组数据,第一行数字n(1<=n<=100000)。
接下来有n个数字,每个数字不超过100000000
Output
出现次数最多的数字和次数。
Example Input

3
1 1 2

Example Output

1 2

Hint

Author
cz
SDUTACM运维技术中心.

#include <bits/stdc++.h>using namespace std;struct node{    int data;    int flag;    int count;} h[100000+10];int num=100000000l;int max_num=0;void  f(int p,int key){    if( h[p].data == key) /// 没有发生占用    {        h[p].flag++;        h[p].count++;    }    else if(h[p].flag==0)    {        h[p].flag++;        h[p].count++;        h[p].data=key;    }    else    {        while(h[p].flag!=0 &&  h[p].data!=key ) ///向后线性移动        {            if( p==(100000-1) )///达到最后一个位置            {                p=0;            }            else p++;        }        if(h[p].flag==0)        {            h[p].flag++;            h[p].count++;            h[p].data=key;        }        else if(h[p].data==key)        {            h[p].flag++;            h[p].count++;        }    }    if(h[p].count >max_num)    {        max_num=h[p].count;        num=key;    }    if(h[p].count == max_num)    {        if(h[p].data < num)            num=h[p].data;    }}int main(){    int n;    cin>>n;///输入个数    for(int i=0; i<100000+10; i++)///初始化    {        h[i].flag=0;        h[i].count=0;        h[i].data=0;    }    for(int i=0; i<n; i++)    {        int t;        cin>>t;        f(t%100000,t);/// 哈希函数(数字对1000000求于)    }    cout<<num<<' '<<max_num<<endl;    return 0;}/********30865328465328965784651545123127845123784651327845123963852789528451852958452845127984651328965986541284651329854185415841854185418541679854296785429867542852852 2*/
0 0
原创粉丝点击