数据结构实验:哈希表
来源:互联网 发布: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
- 数据结构实验-查找-哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表 SDUT
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- 数据结构实验:哈希表
- sdut 数据结构实验:哈希表
- PAT1010
- Android 自定义复合控件之通用标题栏
- JDBC-马士兵教程
- MySQL索引类型 btree索引和hash索引的区别
- Qt4.8.5安装超详细介绍
- 数据结构实验:哈希表
- Atitit 项目文档规范化与必备文档与推荐文档列表
- cocos因遇到未知错误而关闭
- CSS样式(一)
- 03JAVASE
- 事件处理函数
- linux找不到动态库的解决办法
- 谈谈我在创业公司的感悟
- 深入理解Java虚拟机_03虚拟机执行子系统