首页 技术 正文
技术 2022年11月14日
0 收藏 496 点赞 4,965 浏览 1908 个字

11-散列2 Hashing   (25分)

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 TSizeis 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 (≤10​4​​) 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 -平法探测法的训练, 很基础
#include <stdio.h>
#include <stdlib.h>
#include <math.h>#define MAXTABLESIZE 100000int IsPrime(int N);
int Find( int Size, int Key, int* Cells);int main(){
int M, N;
int Tmp, i;
int tmp;
freopen( "C:\\in.txt", "r", stdin );
scanf("%d %d", &M, &N);
if( M>)
while(!IsPrime(M)) M++;
else M = ;
int* Cells = (int*)malloc(M*sizeof(int));
for(i = ; i<M; i++) Cells[i] = ;
for(i = ; i<N; i++){
scanf("%d", &Tmp);
tmp = Find( M, Tmp, Cells);
if(tmp>=)
printf("%d", tmp);
else printf("%c", '-');
if(i!= N-) printf(" ");
}
printf("\n");
free(Cells);
return ;
}int Find( int Size, int Key, int* Cells){
int CurrentPos, NewPos;
int CNum = ;
NewPos = Key%Size;
if( !Cells[NewPos] )
Cells[NewPos] = ;
else
{
for( CNum = ; CNum<Size; CNum++ ){
CurrentPos = (NewPos+CNum*CNum)%Size;
if( !Cells[CurrentPos] ){
Cells[CurrentPos] = ;
NewPos = CurrentPos;
break;
}
}
if(CNum>=Size) NewPos = -;
}
return NewPos;
}int IsPrime(int N){
int p;
for( p = ; p<=(int)sqrt(N); p++ ) {
if(N%p == ) {
p = ;
break;
}
}
return p;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,022
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851