matter
1.我真的不知道为什么最后一个测试点永远过不去。while这种应该怎么改?
2.复习散列表。
code
#include<iostream>
#include<cmath>
using namespace std;
const int MAXN = 10005;
bool is_prime(int a){
if(a <= 1) return false;
int sqr = (int) sqrt(1.0 * a);
for(int i = 2 ; i <= sqr ; i ++){
if(a % i == 0)
return false;
}
return true;
}
bool hashtable[MAXN] = { false };
int main(){
int M_Size , N;
scanf("%d %d" , &M_Size , &N);
//find the smallest prime number which is larger than the size
while(is_prime(M_Size) == false){
M_Size ++;
}
//input
int d[MAXN];
for(int i = 0 ; i < N ; i ++){
scanf("%d" , &d[i]);
}
for(int i = 0 ; i < N ; i ++){
if(hashtable[d[i] % M_Size] == false){
printf("%d" , d[i] % M_Size);
hashtable[d[i] % M_Size] = true;
}
else{
int x = 1 , flag = 1;
while(hashtable[(d[i] + x * x) % M_Size] == true && x <= M_Size){
x ++;
if(x > M_Size){
printf("-");
flag = 0;
break;
}
}
if(flag == 1){
printf("%d" , (d[i] + x * x) % M_Size);
}
}
//no extra space at the end of the line
if(i < N - 1){
printf(" ");
}
}
return 0;
}