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.
随机推荐
- maven基本知识的7个提问
在如今的互联网项目开发当中,特别是Java领域,Maven的仓库管理.依赖管理.继承和聚合等特性为项目的构建提供了一整套完善的解决方案. 这里我们通过7个关于Maven的提问来了解Maven的一些基本 ...
- sql比较字符串,比的到底是什么?
sql里有nvarchar类型的日期数据time1:2019-10-09 00:00:00, 现给定string类型日期time2:2019-10-01 23:59:59,比较两个日期的大小, 发现可 ...
- SQL 增、删、改、查语句
1.SQL SELECT 语句 SELECT语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL SELECT语法 SELECT 列名称 FROM 表名称 以及 SELECT * ...
- 性能监控工具的配置及使用 - Spotlight On Oracle(oracle)
一. Spotlight On Oracle(oracle)1.1. 工具简介Spotlight是一个强有力的Oracle数据库实时性能诊断工具,提供了一个直观的.可视化的数据库活动展现.S ...
- python 函数式编程 闭包,返回一个函数
参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017434209254976 作业 #使用生成器 def createCounter( ...
- FileReader生成图片dataurl的分析
目录 相关代码及html(来源:百度百科) File API及FileReader简介 结合补充知识进行代码分析 修改尝试: 拖曳图片到网页完成转换 相关代码及html(来源:百度百科) <!D ...
- XSS靶场练习
0x00:前言 一个XSS练习平台,闯关形式,一共20关 0x01:开始 第一行都是代码插入点,下面几行是payloads(插入点和payloads中间空一行) LV1 <script>a ...
- Django使用Mysql已存在数据表的方法
在mysql数据库中已经存在有数据的表,自己又不想删除,下面方法可以同步django中创建的表 1.最好将自己建的表名改为前缀和django自动创建表名前缀相同,不改也可以,但是后期表太多容易混乱 2 ...
- <h1>~<h6> 标题标签
<h1>~</h6>标题系列标签 解释:h1到h6 中h1标签最大,h6标签最小,逐一递增. 例如: <h1>标签</h1> <h2>标签& ...
- PHP redis 常用操作
//在列表头部插入一个值one,当列表不存在时自动创建一个列表,key1为列表名 $redis->lpush("key1", "one"); //在列表尾 ...