• 1078 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 -

    题意:

      给出一串数字,要求按照二次方探测法,将这串数字插入到一个大小为Msize的容器中去。如果Msize不是素数,则将其换为比Msize大的最小素数。

    思路:

      做这道题首先应该清楚二次方探测法的定义:如果occupied[n%Msize]没有被占用,则直接将其插入到这个位置。如果这个位置已经被占用,则index = (n + step * step) % Msize; 直到找到没有被占用的位置。注意index的求法不要写成 index = step * step + n % Msize;

    Code :

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int isPrime(int x) {
     6     if (x == 0 || x == 1) return false;
     7     for (int i = 2; i * i <= x; ++i) {
     8         if (x % i == 0) return false;
     9     }
    10     return true;
    11 }
    12 
    13 int main() {
    14     int Msize, n;
    15     cin >> Msize >> n;
    16     while (!isPrime(Msize)) Msize++;
    17 
    18     vector<int> occupied(Msize, 0);
    19     vector<int> pos(Msize, -1);
    20     int temp;
    21     for (int i = 0; i < n; ++i) {
    22         cin >> temp;
    23         for (int j = 0; j < Msize; ++j) {
    24             int index = (j * j + temp) % Msize;
    25             if (!occupied[index]) {
    26                 pos[i] = index;
    27                 occupied[index] = 1;
    28                 break;
    29             }
    30         }
    31     }
    32     for (int i = 0; i < n; ++i) {
    33         if (pos[i] != -1) {
    34             if (i == 0)
    35                 cout << pos[i];
    36             else
    37                 cout << " " << pos[i];
    38         } else {
    39             if (i == 0)
    40                 cout << "-";
    41             else
    42                 cout << " "
    43                      << "-";
    44         }
    45     }
    46     cout << endl;
    47     return 0;
    48 }

     

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Pandas绘图不支持中文解决方案
    MVC模式
    解决import javafx.geometry.Point2D无法导入的问题
    初学linux时遇到的那些哭笑不得的问题
    啊啊我找不到web.xml怎么办呀~~
    解决JSP调用JavaBean出现乱码问题
    设置eclipse自动补全
    ubuntu下eclipse java ee首次打开提示找不到jdk的问题
    android webview 报 [ERROR:in_process_view_renderer.cc(189)] Failed to request GL process. Deadlock likely: 0 问题
    ubuntu创建文件夹桌面快捷方式
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12808839.html
Copyright © 2020-2023  润新知