1078. Hashing (25)-PAT甲级真题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1078. Hashing (25)-PAT甲级真题相关的知识,希望对你有一定的参考价值。

1078. 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 (<=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 –

#include <iostream>
using namespace std;
int table[10001]; 
bool isprim(int n)
{
    if (n == 1)
        return false;
    for (int i = 2; i*i<=n; i++)
    {
        if (!(n%i))
            return false;
    }
    return true;
}
int adjust(int n)
{
    for (int i = n;; i++)
    {
        if (isprim(i))
            return i;
    }
}
int visit[10001];
int main()
{
    int Msize, n;
    cin >> Msize >> n;
    int size = adjust(Msize);
    int temp;

    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        int j = temp%size;
        int step = 0;
        int flag = 0;
        int flag1 = 0;
        int key = 0;
        
        while (visit[j])
        {
            j = (temp+(step*step))%size;
            //j = %size;
            //key = (j + step*step) % size;
            step++;
            //flag = 1;
            if (step > size)
            {
                flag1 = 1;
                break;
            }
        }
        if (flag1 == 1)
            cout <<   << -;
        else
        {
            if (i == 0)
                cout << j;
            else cout <<   << j;
            visit[j] = 1;
        }

        /*
        if (visit[j] == 0)
        {
        visit[j] = 1;
        if (i == 0)
        {
        cout << j;
        }
        else cout << ‘ ‘ << j;
        }
        else
        {
        for (int step = 1; step < n; step++)
        {
        j = (temp%size + step*step)%size;//注意这里,如果使用j,j是一个迭代变量,而其实这里只是希望做一个初始值,step才是迭代变量。

        if (visit[j] == 0)
        {
        visit[j] = 1;
        cout << ‘ ‘ << j;
        flag = 1;
        break;
        }
        }
        if (!flag)
        cout << ‘ ‘ << ‘-‘;
        */
        
    }
}

这道题并不难,有个小坑,在二次探测时,要注意迭代变量。注释那里只需要赋初值,小心当成迭代变量使用。

以上是关于1078. Hashing (25)-PAT甲级真题的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级题分类汇编——杂项

1078. Hashing (25)Hash + 探测——PAT (Advanced Level) Practise

PAT Advanced 1078 Hashing (25) [Hash ?次?探查法]

PAT (Advanced Level) 1078. Hashing (25)

PAT甲级--Hashing (25)

PAT 1078 Hashing[一般][二次探查法]