线性哈希表
来源:互联网 发布:数据割接脚本 编辑:程序博客网 时间:2024/06/05 19:12
数据结构实验之查找七:线性之哈希表
Time Limit: 1000MS Memory limit: 65536K
题目描述
根据给定的一系列整数关键字和素数p,用除留余数法定义hash函数H(Key)=Key%p,将关键字映射到长度为p的哈希表中,用线性探测法解决冲突。重复关键字放在hash表中的同一位置。
输入
连续输入多组数据,每组输入数据第一行为两个正整数N(N <= 1000)和p(p >= N的最小素数),N是关键字总数,p是hash表长度,第2行给出N个正整数关键字,数字间以空格间隔。
输出
输出每个关键字在hash表中的位置,以空格间隔。注意最后一个数字后面不要有空格。
示例输入
5 521 21 21 21 214 524 15 61 884 524 39 61 155 524 39 61 15 39
示例输出
1 1 1 1 14 0 1 34 0 1 24 0 1 2 0
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int hash[6000];
int n,k;
while(cin>>n>>k)
{
memset(hash, -1, sizeof(hash));
for(int i=0; i<n; i++)
{
int a, t;
cin>>a;
t = a%k;
if(hash[t] == -1)
{
cout<<t;
hash[t] =a;
}
else
{
bool Flag = false;
for(int j=0; j<k; j++)
{
if(hash[j]==a)
{
cout<<j;
Flag = true;
break;
}
}
if(!Flag)
{
while(hash[t%k]!= -1)
t++;
cout<<t%k;
hash[t%k]=a;
}
}
if(i == n-1)
cout<<endl;
else
cout<<" ";
}
}
return 0;
}
#include<string.h>
using namespace std;
int main()
{
int hash[6000];
int n,k;
while(cin>>n>>k)
{
memset(hash, -1, sizeof(hash));
for(int i=0; i<n; i++)
{
int a, t;
cin>>a;
t = a%k;
if(hash[t] == -1)
{
cout<<t;
hash[t] =a;
}
else
{
bool Flag = false;
for(int j=0; j<k; j++)
{
if(hash[j]==a)
{
cout<<j;
Flag = true;
break;
}
}
if(!Flag)
{
while(hash[t%k]!= -1)
t++;
cout<<t%k;
hash[t%k]=a;
}
}
if(i == n-1)
cout<<endl;
else
cout<<" ";
}
}
return 0;
}
0 0
- 线性哈希表
- 哈希表---线性探测法
- Linear hashing 线性哈希表
- Linear hashing 线性哈希表
- Linear hashing 线性哈希表
- 线性表、链表、哈希表
- Linear hashing 线性哈希表
- leetcode:线性表、哈希表
- 哈希表线性探测再散列
- 哈希表-线性探测
- 线性之哈希表
- 哈希表(线性探测再散列)
- 线性探测法构建哈希表
- 哈希表线性探测&二次探测
- 实验 哈希表线性探测再散列
- java数据结构 线性表,链表,哈希表
- 线性探测再散列解决冲突的哈希表
- 使用线性探测法构造哈希表
- 【python】sys.argv[] 的用法
- 正则表达式基础语法
- cmake常用命令备忘
- opencv中与split()与merge()的问题
- 计划
- 线性哈希表
- Android自定义捕获Application全局异常
- spring aop
- 查看lib中的函数命令
- 总结的一些公用函数库!
- ios9设置导航状态栏的字体颜色设置
- hdu 1007 Quoit Design(最近点对)
- DISCUZ积分增减底层方法
- Swift中的willSet与didSet