Hash(平方探测法)

11-散列2 Hashing (25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is 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 two positive numbers: MSize (≤10​4​​) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

Sample Input:

4 4
10 6 4 15

Sample Output:

0 1 4 -

 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef enum{Legitimate,Deleted,Empty}EntryType;
typedef int ElementType;
typedef int Position;
typedef struct Element* Cells;
#define MaxSize 15000
struct Element {
	EntryType State;
	ElementType Data;
};
typedef struct Table* HashTable;
struct Table {
	int TableSize;
	Cells Cell;
};
int NextPrimeOrCurrent(int N);
HashTable CreateHashTable(int N);
int Hash(ElementType Key, HashTable H);
void FindAndInsert(int N, HashTable H);
int main()
{
	int MSize, N;
	scanf("%d %d", &MSize, &N);
	HashTable H = CreateHashTable(MSize);
	FindAndInsert(N, H);
}
int NextPrimeOrCurrent(int N)
{
	int k;
	for (k = sqrt(N); k >= 2; k--)
	{
		if (N % k == 0)
			break;
	}
	if (k == 1&&N!=1)
	{
		return N;
	}
	else if (N == 1)
	{
		return 2;
	}
	else 
	{
		N = (N % 2) ? N + 2 : N + 1;
		int i;
		while (N < MaxSize)
		{
			for (i = sqrt(N); i >= 2; i--)
			{
				if (N % i==0)
					break;
			}
			if (i == 1)
				return N;
			else
				N += 2;
		}
	}

}
HashTable CreateHashTable(int N)
{
	int ActualSize = NextPrimeOrCurrent(N);

	HashTable H = (HashTable)malloc(sizeof(struct Table));
	H->TableSize = ActualSize;
	H->Cell = (Cells)malloc(sizeof(struct Element) * H->TableSize);
	for (int i = 0; i < H->TableSize; i++)
	{
		H->Cell[i].State = Empty;
	}
	return H;
}
int Hash(ElementType Key, HashTable H)
{
	return Key % H->TableSize;
}
void FindAndInsert(int N,HashTable H)
{
	ElementType Key;
	Position CurrentPosition,NewPosition;
	int Cnt;
	for (int i = 0; i < N; i++)
	{
		Cnt = 0;
		scanf("%d", &Key);
		CurrentPosition=NewPosition= Hash(Key, H);
		while (H->Cell[NewPosition].State != Empty && H->Cell[NewPosition].Data!= Key)
		{
			Cnt++;
			NewPosition = (CurrentPosition+Cnt*Cnt)% H->TableSize;
			if (Cnt==H->TableSize)
			{
				break;
			}
		}
		if (Cnt == H->TableSize)
		{
			if (i < N - 1)
				printf("- ");
			else
				printf("-");
		}
		else
		{
			H->Cell[NewPosition].State = Legitimate;
			H->Cell[NewPosition].Data = Key;
			if (i < N - 1)
				printf("%d ", NewPosition);
			else
				printf("%d", NewPosition);
		}

	}
}

所犯错误1:NewPosition和CurrentPosition关系

2\1不是素数(应该学一下相应算法,再去做一下进制转换那道题)

3找不到应该如何判断终止条件!!!

4最近有点急躁,大哥慢慢来,总有人比我领先很多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值