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 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 (≤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<cstdio>
const int maxn = ; int hashTable[maxn] = {}; bool isPrime(int n)
{
if (n <= ) return false;
for (int i = ; i * i <= n; i++)
{
if (n % i == )
{
return false;
}
}
return true;
} int main()
{
int n,m;
scanf("%d%d", &m, &n); while(!isPrime(m))
{
m++;
} int pos, temp;
for (int i = ; i < n; i++)
{
scanf("%d",&temp);
pos = temp % m;
if (!hashTable[pos])
{
hashTable[pos] = ;
printf("%d", pos);
}
else
{
int step = ;
for (; step <= m; step++)
{
pos = (temp + step * step) % m;
if (!hashTable[pos])
{
hashTable[pos] = ;
printf("%d", pos);
break;
}
}
if (step >= m)
{
printf("-");
}
} if (i < n - )
{
printf(" ");
}
} return ;
}
11-散列2 Hashing (25 分)的更多相关文章
- PTA 字符串关键字的散列映射(25 分)
7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...
- PTA 11-散列2 Hashing (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing (25分) The task of this pro ...
- pat09-散列1. Hashing (25)
09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PTA 逆散列问题 (30 分)(贪心)
题目链接:https://pintia.cn/problem-sets/1107178288721649664/problems/1107178432099737614 题目大意: 给定长度为 N 的 ...
- JavaScript数据结构-11.散列
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 5-17 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT 列车厢调度 (25分)(栈和容器的简单应用)
1 ====== <--移动方向 / 3 ===== \ 2 ====== -->移动方向 大家或许在某些数据结构教材上见到过“列车厢调度问题”(当然没见过也不要紧).今天,我们就来实际操 ...
- 纯数据结构Java实现(11/11)(散列)
欢迎访问我的自建博客: CH-YK Blog.
随机推荐
- golang学习笔记 --go test
Go语言拥有一套单元测试和性能测试系统,仅需要添加很少的代码就可以快速测试一段需求代码. go test 命令,会自动读取源码目录下面名为 *_test.go 的文件,生成并运行测试用的可执行文件.输 ...
- Angulaur导入其他位置的样式
建立一个统一样式文件base-xxx.component.css 在需要导入样式的组件中,编辑.ts文件导入样式: 右侧是它的相对路径.
- axios FastMock 跨域 代理
发送请求: 实现:发送请求,获取数据. 原本想自己写服务,后来无意间找到FastMock这个东东,于是就有了下文... 首先我安装了axios,在fastmock注册好了并创建了一个接口,怎么搞自行百 ...
- C# 获取所有可用的打印机
C# 获取所有安装了的打印机代码如下: using System.Drawing.Printing; var printers = PrinterSettings.InstalledPrinters; ...
- spring事务概念与获取事务时事务传播行为源码分析
一.事务状态:org.springframework.transaction.TransactionStatus isNewTransaction 是否是新事务 hasSavepoint 是否有保存点 ...
- python 学习之 基础篇二 字符编码
声明: 博文参考1:字符编码发展历程(ASCII,Unicode,UTF-8) 博文参考2:Python常见字符编码间的转换 (1)为什么要用字符编码 早期的计算机使用的是通电与否的特性的真空管,如果 ...
- UUID生成库libuuid和crossguid
libuuid是一个开源的用于生成UUID(Universally Unique Identifier,通用唯一标识符)的库. 可从https://sourceforge.net/projects/l ...
- html5表单上传控件Files筛选指定格式的文件:accept属性过滤excel文件
摘自:http://blog.csdn.net/jyy_12/article/details/9851349 (IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允 ...
- CMake相关代码片段
目录 用于执行CMake的.bat脚本 CMakeLists.txt和.cmake中的代码片段 判断平台:32位还是64位? 判断Visual Studio版本 判断操作系统 判断是Debug还是Re ...
- (原+修改)ubuntu上离线安装pytorch
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/12000809.html 参考网址: https://blog.csdn.net/qq_4193655 ...