博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pat09-散列1. Hashing (25)
阅读量:5122 次
发布时间:2019-06-13

本文共 2406 字,大约阅读时间需要 8 分钟。

09-散列1. Hashing (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 410 6 4 15
Sample Output:
0 1 4 -

 

注意边界测试!

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 bool h[10005]; 9 int GetNextPrime(int num){10 int i,j;11 if(num==1){ //注意边界12 return 2;13 }14 if(num==2){ //注意边界15 return 2;16 }17 for(i=num;;i++){18 if(i%2==0){19 continue;20 }21 for(j=3;j*j<=i;j+=2){22 if(i%j==0){23 break;24 }25 }26 if(j*j>i){27 return i;28 }29 }30 }31 int main(){32 //freopen("D:\\INPUT.txt","r",stdin);33 int msize,n;34 scanf("%d %d",&msize,&n);35 int i,num,j;36 memset(h,false,sizeof(h));37 msize=GetNextPrime(msize);38 queue
q;39 for(i=0;i
msize/2){57 q.push(-1);58 }59 }60 }61 int now;62 if(!q.empty()){63 now=q.front();64 q.pop();65 if(now==-1){66 printf("-");67 }68 else{69 printf("%d",now);70 }71 }72 while(!q.empty()){73 now=q.front();74 q.pop();75 if(now==-1){76 printf(" -");77 }78 else{79 printf(" %d",now);80 }81 }82 printf("\n");83 return 0;84 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4729919.html

你可能感兴趣的文章
为什么匿名内部类参数必须为final类型
查看>>
Codeforces 629 A. Far Relative’s Birthday Cake
查看>>
【框架学习与探究之日志组件--Log4Net与NLog】
查看>>
操作execl
查看>>
UESTC_导弹拦截 2015 UESTC Training for Dynamic Programming<Problem N>
查看>>
工作用到的SQL语句
查看>>
POJ1002 487-3279
查看>>
sqlserver 生成脚本执行创建索引
查看>>
权益保护-产权保护:专利申请
查看>>
【计算机网络】第二章 网络应用(4)
查看>>
pyqt5-QPlainTextEdit普通文本
查看>>
短信验证码js
查看>>
hadoop学习第二天之伪分布模式安装(下)
查看>>
初学微信小程序 TodoList
查看>>
如何在 vuex action 中获取到 vue 实例
查看>>
Windows绘图中的GDI映射模式
查看>>
MYSQL5.7:几个简单的show语句演示
查看>>
vim 把满足条件的数字进行加上一些数字
查看>>
●枚举、递归
查看>>
使用LSTM和Softmx来进行意图识别
查看>>