您的位置:首页 > 理论基础 > 数据结构算法

3379 数据结构实验之查找七:线性之哈希表

2016-12-11 21:32 337 查看
数据结构实验之查找七:线性之哈希表

#include <bits/stdc++.h>

using namespace std;

int main()
{
int Hash[5510];
int n,k;
while(cin>>n>>k)
{
memset(Hash, -1, sizeof(Hash));
for(int i=0; i<n; i++)
{
int x, t;
cin>>x;
t = x % k;
if(Hash[t] == -1)
{
cout<<t;
Hash[t] = x;
}
else
{
bool Flag = false;
for(int j=0; j<k; j++)
{
if(Hash[j] == x)
{
cout<<j;
Flag = true;
break;
}
}
if(!Flag)
{
while(Hash[t % k] != -1)
t++;
cout<<t%k;
Hash[t%k] = x;
}
}
if(i == n-1)
cout<<endl;
else
cout<<" ";
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: