先挂上题目~https://www.patest.cn/contests/pat-a-practise/1078
Hash+Quadratic probing(平方探测。。英语真的很重要。。)
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 11100;
bool hashtable[maxn];
int cnt=0,msize,n,hkey;
bool isPrime(int x){
if(x == 1) return false;
for(int i = 2; i*i<=x; i++)
if(x%i == 0) return false;
return true;
}
void solve(int num){
hkey = num%msize;
int i = 1;
int tem = hkey;//平方探测,+1方,+2方...注意是在原来的基础上进行不是累加,所以要保存hkey值
while(i<msize && hashtable[hkey]){
hkey = (tem+i*i)%msize;
i++;
}
if(hashtable[hkey]){
printf("-");
return ;
}
hashtable[hkey] = true;
printf("%d", hkey);
}
int main(){
memset(hashtable, false, sizeof(hashtable));
scanf("%d%d", &msize, &n);
while(!isPrime(msize)) msize++;
int num;
for(int i = 0; i<n; i++){
scanf("%d", &num);
solve(num);
if(i != n-1) printf(" ");
}
return 0;
}