1078. 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 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 -
#include<stdio.h>
#include<math.h> int main()
{
int i,MSize,n,j;
bool isprime[];
bool isexit[];
isprime[] = false;
for(i= ;i<=;i++)
{
bool is = true;
for(j=;j<=sqrt(i*1.0);j++)
{
if(i % j == )
{
is = false;
break;
}
} isprime[i] = is;
//if(is) printf("%d ",i);
}
while(scanf("%d%d",&MSize,&n)!=EOF)
{
while(isprime[MSize]==false)
++MSize; for(i = ;i<MSize ;i++)
isexit[i] = false; bool fir =true;
for(i = ;i<n;i++)
{
int tem;
scanf("%d",&tem);
if(isexit[tem%MSize]==false)
{
if(fir)
{
printf("%d",tem%MSize);
fir = false;
}
else
{
printf(" %d",tem%MSize);
}
isexit[tem%MSize] = true;
}
else
{
bool find = false;
for(j = ;j <MSize;j++)
{
if(isexit[(tem+j*j)%MSize]==false)
{
find = true;
printf(" %d",(tem+j*j)%MSize);
isexit[(tem+j*j)%MSize] = true;
break;
}
} if(!find) printf(" -");
}
} printf("\n");
}
return ;
}
1078. Hashing (25)的更多相关文章
- 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT甲题题解-1078. Hashing (25)-hash散列
二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- 1078 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- pat1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
随机推荐
- js判断图片上传时的文件大小,和宽高尺寸
今天在做图片上传的小功能,使用了一个kissy上传组件.很好奇它是如何在图片上传前,检测到图片的大小和尺寸的?我们来写个小实例实现一下吧 如何读取图片的size 首先,原生input file控件有个 ...
- [转]Oracle hang分析
hanganalyze是ORACLE的一款性能诊断工具,这个款工具是从oracle 8.0.6开始可用,在oracle数据库出现严重的性能问题的时候它可以帮助你定位问题所在. 1.首先说说hangan ...
- ARM Linux bootloader笔记
.text //指定了后续编译出来的内容放在代码段[可执行] .global //告诉编译器后续跟的是一个全局可见的名字[可能是变量,也可以是函数名] _start /*函数的其实地址,也是编译.链接 ...
- logstash jdbc 各种数据库配置
MySQL数据库 Driver ="path/to/jdbc-drivers/mysql-connector-java-5.1.35-bin.jar" //驱动程序Class ...
- javaweb学习总结九(xml解析以及调整JVM内存大小)
一:解析XML文件的两种方式 1:dom,document object model,文档对象模型. 2:sax,simple API for XML. 3:比较dom和sax解析XML文件的优缺点 ...
- poj 2749 2-SAT问题
思路:首先将hate和friend建边求其次2-SAT问题,判断是否能有解,没解就输出-1,否则用二分枚举最大的长度,将两个barn的距离小于mid的看做是矛盾,然后建边,求2-SAT问题.找出最优解 ...
- Jedis - hello world
Maven Dependency: <dependency> <groupId>redis.clients</groupId> <artifactId> ...
- python学习day5--set、函数
1.set 无序,不重复序列 创建:与dict一样用{},区别在于dict内元素为键值对 se={"123","456,444"} print(type(se) ...
- 转:SqlServer2008误操作数据(delete或者update)后恢复数据
Sqlserver2008误操作数据(delete或者update)后恢复数据(转) 实际工作中,有时会直接在数据库中操作数据,比如对数据进行delete或者update操作,当进行这些操作的时候,如 ...
- Android之点击切换图片
package com.example.SlidePictures; import java.util.Timer; import java.util.TimerTask; import com.ex ...