PTA甲级 1078 Hashing (25分)-散列+素数


强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

PS

题目原文

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 (≤104) 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 -

生词如下:

hash table 散列表

Quadratic 二次方程式

probing 探查

distinct 有区别的

关键句子:

Quadratic probing (with positive increments only) is used to solve the collisions.

只能采用二次探查法,而且都是正增量.

思路:

① 直接套用素数函数-这里注意1不是素数.

② 要看得懂题目,题目都讲了要用二次探测法.

代码如下:

这个是我的代码,

#include<iostream>
#include<vector>
using namespace std;
//判断是否是素数的函数
bool isPrime(int);
int main(void) {
	int M, N;
	cin >> M >> N;
	while (!isPrime(M)) { ++M; }
	//cout << M;
	vector<bool> Data(M, false);
	bool first=false;
	for (int i = 0; i < N; ++i) {
		int origan = 0,t=0;
		cin >> origan;
		t = origan % M;
		
		if (!Data[t]) {
			Data[t] = true;
			if (!first)	first = true;
			else  cout << " ";
			cout << t;
		}
		else {
			bool Isput = false;
			//关键的二次探测法的代码.
			for (int i = 1; i <=M-1; ++i) {
				t = (origan + i * i) % M;
				if (!Data[t]) {
					Data[t] = true;
					cout << " "<<t;
					Isput = true;
					break;
				}
			}
			if (!Isput)	cout << " -";
		}
	}
	return 0;
}
bool isPrime(int M) {
	if (M <= 1)	return false;
	for (int i = 2; i * i <= M; i++) {
		if (M % i == 0)	return false;
	}
	return true;
}

下面请一起欣赏柳神的代码.

#include <iostream>
using namespace std;
int Magnitude, n, hashTable[10100];
bool isprime(int num) {
    if (num == 1) return false;
    for (int i = 2; i * i <= num; i++)
        if (num % i == 0) return false;
    return true;
}
//插入函数,柳神还是柳神,从0开始就可以少些一段的代码.而且其实本质都是相同的.
//我写的麻烦了.
void insert(int key) {
    for (int step = 0; step < Magnitude; step++) {
        int index = (key + step * step) % Magnitude;
        if (hashTable[index] == 0) {
            hashTable[index] = 1;
            cout << index % Magnitude;
            return;
        }
    }
    cout << '-';
}
int main() {
    cin >> Magnitude >> n;
    while (!isprime(Magnitude)) Magnitude++;
    for (int i = 0; i < n; i++) {
        int key;
        cin >> key;
        if (i != 0) cout << ' ';
        insert(key);
    }
    return 0;
}

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

在这里插入图片描述

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页