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.
随机推荐
- Window安装AutoCAD
1.运行crack文件夹下的“nlm11.14.1.3_ipv4_ipv6_win64.msi”,根据向导提示完成安装2.同样在“MAGNiTUDE”文件夹中找到“adesk.dat”文件,用记事本打 ...
- 基础知识---IEnumerable、ICollection、IList、IQueryable
一.定义 IEnumerable public interface IEnumerable<out T> : IEnumerable ICollection public interfac ...
- 在Windows10 安装 Linux 子系统
在工作中我们经常需要在Windows中安装Linux系统来完成一些工作,通常使用VMware.virtualpc,Hyper-V等虚拟化技术来实现,Now,我们有了更便利的方法来实现,这就是Windo ...
- kafka 解密:破除单机topic数多性能下降魔咒
https://bbs.huaweicloud.com/blogs/112956 版权归PUMA项目组所有,转载请声明,多谢. kakfa大规模集群能力在前面已给大家分享过,kafka作为消息总线,在 ...
- JavaScaript学习笔记第(一)
js由三部分组成,分别是ECMAScript.DOM.BOM 其中ECMAScript规定了js的语法 js是一门解释型语言.脚本语言.动态类型语言.基于对象语言 书写js代码和CSS一样,有三个书写 ...
- alpine安装sshd/ssh server
1.下载alpine镜像 1 2 3 4 5 6 7 8 9 10 [root@docker43 ~]# docker pull alpine Using default tag: latest Tr ...
- python 正则表达式、re
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. 参考链接:https://www.runoob.com/python/python-reg-expressions. ...
- 我为啥不想用Python
Python这门语言从一开始就是一个玩具语言,它不是给正经程序员用的东西. 运行效率低 Python运行效率很低,这就导致Python中很多库底层实际上是C++.很多时候,自己千方百计优化的结果就不如 ...
- 攻防世界-web -高手进阶区-PHP2
题目 首先发现源码泄露 /index.phps 查看源代码 即: <?php if("admin"===$_GET[id]) { echo("<p>no ...
- Java 之 Set 源码分析
一.HashSet 构造方法: HashSet() :构造一个新的空 set,其底层 HashMap 实例的默认初始容量是 16,加载因子是 0.75 源码: 二.LinkedHashSet 构造方 ...