国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 二连浩特市| 桐城市| 溧水县| 通江县| 衡阳县| 毕节市| 利津县| 江北区| 延长县| 琼结县| 东台市| 海兴县| 平定县| 邮箱| 望奎县| 金平| 姜堰市| 上杭县| 达拉特旗| 长治市| 布拖县| 嫩江县| 比如县| 闵行区| 义马市| 从江县| 礼泉县| 南涧| 丰镇市| 桐庐县| 上栗县| 调兵山市| 夏邑县| 盱眙县| 贵南县| 小金县| 大英县| 新绛县| 乳山市| 大石桥市| 周宁县|