1078. Hashing (25)
来源:互联网 发布:大数据的优缺点 编辑:程序博客网 时间:2024/06/06 05:06
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 15Sample Output:
0 1 4 -
平方探测法,取正。
while很妙
#include<stdio.h>#include<math.h>int size,n;int map[10010];int isprime(int x){if(x<2){return 0;}int i,sqr=sqrt(x);for(i=2;i<=sqr;i++){if(x%i==0){return 0;}}return 1;}int setprime(int x){int i;for(i=x+1;;i++){if(isprime(i)){return i;}}}int main(){int i,j;scanf("%d %d",&size,&n);if(isprime(size)==0){size=setprime(size);}//while(isprime(size)==0){//size++;//}int temp,x;for(i=0;i<n;i++){scanf("%d",&x);temp=x%size;if(map[temp]==0){map[temp]=1;printf("%d",temp);}else{for(j=1;j<=size;j++){temp=(x+j*j)%size;if(map[temp]==0){map[temp]=1;printf("%d",temp);break;}}if(j==size+1){printf("-");}}if(i<n-1){printf(" ");}}}
- 1078. Hashing (25)
- PAT 1078. Hashing (25)
- pat 1078. Hashing (25)
- PAT 1078. Hashing (25)
- PAT 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- PAT 1078. Hashing (25)
- PAT 1078. Hashing (25)
- 1078. Hashing (25)
- PAT 1078. Hashing (25)
- 1078. Hashing (25)
- 【PAT】1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 【unity学习笔记】unity优化DrawCall 及动态批处理的约束
- 异步请求时,JSON.stringify()导致data参数中加号(+)丢失
- Crash加载ramdump报cpu_possible_mask异常问题
- Java例4.5电视节目
- 喵哈哈村的嘟嘟熊魔法(四)(set+对组)
- 1078. Hashing (25)
- http并发,操作系统如何识别对应的进程,线程请求
- canvas五角星(五星红旗)绘制
- [Unity优化]Unity Android Profiler
- 数据结构与算法简记:线索化二叉树
- TeamTalk源码分析(九) —— 服务器端route_server源码分析
- jqzoom图片放大器代码
- error: Microsoft Visual C++ 9.0 is required
- 字符串练习