国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

PAT 1078.Hashing (25)

2019-11-08 03:00:02
字體:
來源:轉載
供稿:網友

題目: 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 - 題意:給出散列表Tsize和想要插入的元素, 將這些元素按讀入的順序插入散列表中,其中散列函數H(key) = key%Tsize,解決沖突采用正向增加的二次探查法,此外,如果題目給出的Tsize不是素數,那么需要將Tsize重新賦值一個比Tsize大的素數再進行元素插入。

代碼:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;const int maxn = 10007 + 10;bool flag[maxn] = {false};bool isPrime(int x) { if(x <= 1) return false; int midNumber = (int)sqrt(x*1.0); for(int i = 2; i <= midNumber; i++) { if(x%i == 0) return false; } return true;}int main(){ int Msize, N; scanf("%d%d", &Msize, &N); while(true) { if(isPrime(Msize)) break; else { Msize++; } } int input; int indexNumber = 0; for(int i = 0; i < N; i++) { scanf("%d", &input); int mod = input%Msize; if(flag[mod] == false) { flag[mod] = true; if(indexNumber == 0) { printf("%d", mod); } else { printf(" %d", mod); } indexNumber++; } else { int j = 1; for(; j <= Msize; j++) { int v = (input + j * j)%Msize; if(flag[v] == false) { flag[v] = true; if(indexNumber == 0) { printf("%d", v); } else { printf(" %d", v); } indexNumber++; break; } } if(j > Msize) { if(indexNumber == 0) printf("-"); else printf(" -"); } } } printf("/n"); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万山特区| 延长县| 寻甸| 胶州市| 裕民县| 万年县| 丰台区| 新津县| 宁南县| 吴忠市| 那曲县| 沙湾县| 忻城县| 龙南县| 湟源县| 保康县| 屏南县| 通江县| 泗阳县| 邛崃市| 泊头市| 郯城县| 建昌县| 临朐县| 山丹县| 巴林右旗| 昌吉市| 白水县| 岫岩| 额尔古纳市| 葫芦岛市| 东宁县| 湘西| 永安市| 平南县| 太原市| 德保县| 比如县| 华容县| 宜良县| 北海市|