国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朔州市| 九龙坡区| 榆社县| 齐齐哈尔市| 海城市| 天柱县| 安庆市| 南昌县| 齐河县| 蓬安县| 湛江市| 柘城县| 元氏县| 铁力市| 宁海县| 滕州市| 富平县| 龙门县| 银川市| 海城市| 克什克腾旗| 兴安盟| 五峰| 峡江县| 黄龙县| 逊克县| 邓州市| 水富县| 石河子市| 静乐县| 客服| 杂多县| 盐亭县| 呈贡县| 大邑县| 乌兰浩特市| 普兰店市| 济源市| 陆良县| 天全县| 东莞市|