#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
bool isp(int a)
{
if(a==1)
return 0;
for(int i=2;i<=(int)sqrt(a);i++)
{
if(a%i==0)
return 0;
}
return 1;
}
int Hash[10005]={0};
int main()
{
// freopen("in.txt","r" ,stdin);
int Tsize,N;
scanf("%d %d",&Tsize,&N);
while(isp(Tsize)==0)
Tsize++;
for(int i=0;i<N;i++)
{
int t;
scanf("%d",&t);
int pos=t%Tsize;
if(Hash[pos]==0)
{
Hash[pos]=1;
printf("%d",pos);
if(i!=N-1)
printf(" ");
}
else
{
int j;
for(j=1;j<Tsize;j++)
{
pos=(t+j*j)%Tsize;
if(Hash[pos]==0)
{
Hash[pos]=1;
printf("%d",pos);
if(i!=N-1)
printf(" ");
break;
}
}
if(j==Tsize)
{
printf("-");
if(i!=N-1)
printf(" ");
}
}
}
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
bool isp(int a)
{
if(a==1)
return 0;
for(int i=2;i<=(int)sqrt(a);i++)
{
if(a%i==0)
return 0;
}
return 1;
}
int Hash[10005]={0};
int main()
{
freopen("in.txt","r" ,stdin);
int Tsize,N;
scanf("%d %d",&Tsize,&N);
while(isp(Tsize)==0)
Tsize++;
for(int i=0;i<N;i++)
{
int t;
scanf("%d",&t);
int pos=t%Tsize;
if(Hash[pos]==0)
{
Hash[pos]=1;
printf("%d",pos);
if(i!=N-1)
printf(" ");
}
else
{
int j;
for(j=1;j<Tsize;j++)
{
pos=(t+j*j)%Tsize;
if(Hash[pos]==0)
{
Hash[pos]=1;
printf("%d",pos);
if(i!=N-1)
printf(" ");
break;
}
}
if(j==Tsize)
{
printf("-");
if(i!=N-1)
printf(" ");
}
}
}
return 0;
}
1078. Hashing (25)
最新推荐文章于 2020-04-20 12:13:01 发布
本文介绍了一个使用素数探测法解决哈希冲突的算法,并通过C++实现了一个简单的哈希表应用实例。该算法首先寻找合适的素数作为哈希表大小,接着采用二次探测法解决哈希冲突。

1996

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



