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

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

1078. Hashing (25)

2019-11-11 01:50:22
字體:
來源:轉載
供稿:網友

題目鏈接: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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吕梁市| 江门市| 乌鲁木齐县| 金溪县| 土默特左旗| 余江县| 和龙市| 兴宁市| 长宁区| 长沙市| 时尚| 克山县| 无棣县| 拜泉县| 财经| 宁晋县| 乌拉特后旗| 迭部县| 富顺县| 鄂伦春自治旗| 定南县| 红原县| 罗江县| 正安县| 安塞县| 清徐县| 利辛县| 莱阳市| 山阴县| 和林格尔县| 金湖县| 额尔古纳市| 青田县| 辽宁省| 高州市| 高台县| 梁平县| 吴川市| 满城县| 皋兰县| 佛坪县|