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

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

1078. Hashing (25)

2019-11-10 23:16:44
字體:
來源:轉載
供稿:網友

題目鏈接: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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武定县| 神池县| 淅川县| 姚安县| 商都县| 漾濞| 苏州市| 松桃| 泰顺县| 泰来县| 连云港市| 太保市| 瑞安市| 抚松县| 张家口市| 秦皇岛市| 岐山县| 丽江市| 安远县| 东阳市| 开远市| 唐山市| 乐昌市| 祁阳县| 轮台县| 神农架林区| 台北市| 邹城市| 左贡县| 青河县| 容城县| 都兰县| 茶陵县| 安溪县| 滦南县| 滨海县| 苍溪县| 廉江市| 廉江市| 三门县| 司法|