#include<bits/stdc++.h>
using namespace std;
void hashf(int n,int p)
{
int hash0[5510];
memset(hash0,-1,sizeof(hash0));
for(int i=0; i<n; i++)
{
int ins,t;
scanf("%d",&ins);
t=ins%p;
if(hash0[t]==-1)
{
printf("%d",t);
hash0[t]=ins;
}
else
{
bool f=false;
for(int j=0; j<p; j++)
if(hash0[j]==ins)
{
printf("%d",j);
f=true;
break;
}
if(!f)
{
while(hash0[t%p]!=-1)
t++;
printf("%d",t%p);
hash0[t%p]=ins;
}
}
if(i!=n-1)printf(" ");
else
printf("\n");
}
}
int main()
{
int n,p;
while(~scanf("%d%d",&n,&p))
hashf(n,p);
return 0;
}
SDUT3379数据结构实验之查找七:线性之哈希表
最新推荐文章于 2020-04-16 22:01:11 发布
本文介绍了一种基于哈希表的数据结构实现方法,并通过一个具体的C++代码示例展示了如何进行哈希插入操作。该示例使用了线性探测解决冲突的方法。

1707

被折叠的 条评论
为什么被折叠?



