甲级PAT 1145 Hashing - Average Search Time (25 分)(hash,二次方探测)

1145 Hashing - Average Search Time (25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be H(key)=key%TSize where TSizeis the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 10​4​​. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 10​5​​.

Output Specification:

For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

题目要求

一个hash表,其容量是大于等于输入的msize的最小素数,然后将n个序列插入hash表中,如果有元素无法插入hash表中,则输出提示信息

然后输入m个查询数,求出它们平均查找的次数。

解题思路

首先是插入到hash表中,

10%5=0

6%5=1

15%5=0

(15+1^2)%5=1

(15+2^2)%5=4

(15+3^2)%5=4

(15+4^2)%5=1

(15+5^2)%5=0 (这里要取到msize)

15无法插入

11%5=1

(11+1^2)%5=2

可以得到如下hash表

0 1 2 3 4
10 6 11   4

查找

11%5=1  有元素6,但不等于11

(11+1^2)%5=2 等于11

11查找了2次

4%5=4 等于4

4查找了1次

15%5=0 有元素10,但不等于15

(15+1^2)%5=1 有元素6,但不等于15

(15+2^2)%5=4 有元素4,但不等于15

(15+3^2)%5=4 有元素4,但不等于15

(15+4^2)%5=1 有元素6,但不等于15

(15+5^2)%5=0  有元素10,但不等于15

15查找了6次

2%5=2  有元素11,但不等于2

(2+1^2)%5=3 没有元素,说明2不在hash表中

2查找了2次

一共2+1+6+2=11次 ,11/4=2.8 所以这就是为什么要取到Msize,不然结果就不对

完整代码

#include
using namespace std;
#define maxn 10010

int hashtable[10010];

bool isprime(int x){
  for(int i=2;i<=sqrt(x);i++){
    if(x%i==0) return false;
  }  
  return true;
}

bool insert(int x,int m){
  int i;
	for(i=0;i<=m;i++){
		int key=(x+i*i)%m;
		if(hashtable[key]==0){
			hashtable[key]=x;
			break;
		}
	}
	if(i==m+1) return false;
	else return true;
}


int main(){
	int msize,n,k,i,j,x,num=0;
	double sum=0;
	cin>>msize>>n>>k;
	memset(hashtable,0,sizeof(hashtable));
	while(!isprime(msize)) msize++;
	for(i=0;i>x;
		if(!insert(x,msize)) printf("%d cannot be inserted.\n",x);
	}
	for(i=0;i>x;
		for(j=0;j<=msize;j++){
			sum+=1.0;
			int key=(x+j*j)%msize;
			if(hashtable[key]==x || hashtable[key]==0) break;
		}
	}
	printf("%.1lf",sum/k);
	return 0;
}

注意,记录一下之前的坑,最开始写的找到hash的容量时,按如下所写

int getminprime(int x){
	int i,j;
	for(i=x;i<=maxn;i++){
		for(j=2;j

然后测试点1报浮点数错误,想了想明白了,原来是这里x如果等于0的话就没有返回值了。。在后面会出现%0的情况。

你可能感兴趣的:(PAT甲级)