11-散列2 Hashing

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 ( 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 () and N () 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<stdlib.h>
#include<stdio.h>
int insert(int *b, int number,int n) {
	int ns = number % n;
	if (b[ns] == -1) {
		b[ns] = number;
		return ns;
	}
	else {
		int i = 1;
		int j = 1;
		ns+=j;
		int s=0;
		while (s<n)
		{
		  if(b[ns]==-1){
		    b[ns]=number;
		    return ns;
		  }
			ns -= j;
			i++;
			j = i * i;
			ns += j;
			s++;
			while(ns>=n)ns-=n;
		}
		return -1;
		if (ns >= n || b[ns] != -1) {
			return -1;
		}
		else {
			b[ns] = number;
			return ns;
		}
	}
}


int main()
{
	int a[100000] = { 0 };
	a[0] = 1, a[1] = 1;
	for (int i = 2; i < 100000; i++)
	{
		if (a[i] == 0) {
			int j = 2 * i;
			while (j<100000)
			{
				a[j] = 1;
				j += i;
			}
		}
	}

	int n,cnt;
	scanf("%d %d", &n,&cnt);
	while (a[n]==1)
	{
		n++;
	}
	int b[100000];
	for (int i = 0; i < n; i++)
	{
		b[i] = -1;
	}
	int numbers;
	for (int i = 0; i < cnt; i++)
	{
		scanf("%d", &numbers);
		int tep;
		tep = insert(b, numbers, n);
		if (i < cnt - 1) {
			if (tep != -1) {
				printf("%d ", tep);
			}
			else {
				printf("- ");
			}
		}
		else {
			if (tep != -1) {
				printf("%d", tep);
			}
			else {
				printf("-");
			}
		}
	}
	system("pause");
    return 0;
}
这里要注意:正向扫描并不是扫描一遍,越界后求余数继续扫描直到都被扫描过
数据结构第16次作业,hash表 Spellchecking Prerequisites, Goals, and Outcomes Prerequisites: Students should have mastered the following prerequisite skills. • Hash Tables - Understanding of the concept of a recursive function • Inheritance - Enhancing an existing data structure through specialization Goals: This assignment is designed to reinforce the student's understanding of the use of hash tables as searchable containers. Outcomes: Students successfully completing this assignment would master the following outcomes. • Familiarize how to use hash tables, specifically hash sets Background Any word processing application will typically contain a spell check feature. Not only does this feature point out potentially misspelled words; it also suggests possible corrections. Description The program to be completed for this assessment is a spell checker. Below is a screen shot of the program in execution? The program begins by opening a word list text file, specified by a command line parameter. The program outputs an error message and terminates if it cannot open the specified word list text file. A sample word list text file (wordlist.txt) is given in the supplied wordlist.zip archive. After successfully opening the specified word list text file, the program then stores each word into a hash table. The program then opens a file to spell check. This user specifies this file through the command line. After opening this file, the program then compares each word in the file against the words stored in the hash table. The program considers a word to be misspelled if the word does not exist in the hash table. When this occurs, the program displays the line number the word appeared in, the word, and a list of possible corrections. The list of possible corrections for a misspelled word is generated using a simple algorithm. Any variation of a misspelled word that is itself a word (i.e. it is found in the word list file) is a possible correction. Your solution to this asses
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值