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

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

1078. Hashing (25)

2019-11-11 00:24:19
字體:
來源:轉載
供稿:網友

題目鏈接:https://www.patest.cn/contests/pat-a-PRactise/1078 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 - 注:Quadratic probing是指二次方探查法,即當H(a)發生沖突時,讓a+1*1,a+2*2,a+3*3…..(step每次加1,step

#include<cstdio>const int maxs=10010;int a[maxs];bool occupied[maxs]={false};bool isPrime(int x){ bool flag=true; if(x<=1) flag=false; for(int i=2;i*i<=x;i++){ if(x%i==0){ flag=false; break; } } return flag;}int main(){ int msize,n; scanf("%d %d",&msize,&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } while(isPrime(msize)==false){ msize++; } for(int i=0;i<n;i++){ int t=a[i]%msize; if(occupied[t]==false){ printf("%d",t); occupied[t]=true; }else{ int step=1; for(step=1;step<msize;step++){ int m=(a[i]+step*step)%msize; if(occupied[m]==false){ printf("%d",m); occupied[m]=true; break; } } if(step==msize){ printf("-"); }// while(occupied[(a[i]+step*step)%msize]==true&&step<msize){// step++;// }// if(step<msize){// printf("%d",(a[i]+step*step)%msize);// }else{// printf("-"); // } } if(i<n-1) printf(" "); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 漠河县| 桦南县| 西青区| 汽车| 海安县| 宝坻区| 西充县| 五原县| 阳曲县| 浏阳市| 灵武市| 子洲县| 昌都县| 周至县| 永清县| 隆化县| 湟中县| 叙永县| 昌图县| 迁安市| 外汇| 贵港市| 鄢陵县| 武邑县| 田东县| 涪陵区| 女性| 昌邑市| 资中县| 吴忠市| 柞水县| 博罗县| 那坡县| 叙永县| 威海市| 青田县| 永寿县| 宝坻区| 沭阳县| 将乐县| 自贡市|