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

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

1078. Hashing (25)

2019-11-11 00:31:50
字體:
來源:轉載
供稿:網友

題目鏈接: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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北海市| 嘉义县| 通道| 商水县| 县级市| 惠水县| 聂荣县| 沐川县| 灵寿县| 离岛区| 贡嘎县| 靖西县| 赤城县| 焉耆| 会昌县| 石林| 临沧市| 岳池县| 介休市| 乐山市| 张北县| 获嘉县| 夹江县| 保定市| 图片| 叙永县| 西丰县| 北票市| 西城区| 如皋市| 岱山县| 肥乡县| 翁源县| 绥化市| 柞水县| 手机| 班玛县| 石棉县| 广东省| 遂川县| 贡山|