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.
随机推荐
- The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.
springboot 表单体积过大时报错: The multi-part request contained parameter data (excluding uploaded files) tha ...
- Android开发中网络代理设置实用总结
一.背景 进行Android项目开发时,跟网络代理基本上天天都在打交道.通常情况下,至少有三个场景中经常用到网络代理: 1,经常通过Chrome访问Google等国外的技术网站,如通过SS工具等: 2 ...
- 图解微信小程序---scroll_view实现首页排行榜布局
图解微信小程序---scroll_view实现首页排行榜布局 什么是scroll-view? 滚动视图可滚动视图区域.使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 h ...
- 【07】Kubernets:资源清单(控制器 - DaemonSet)
写在前面的话 前面讲解了 Pod / ReplicaSet / Deployment 的资源清单,我们这里谈一下 DaemonSet 的资源清单. 之前说过,DaemonSet 控制器能够保证资源在每 ...
- ES6的强大变量声明
ES6是javascript的新特性,今天来说说声明变量 过去我们声明变量,都是一个一个声明,现在有了一种新的声明方式,它可以将一个多个变量同时声明,声明后变量同时存在一个集合中,集合的数据类型是对象 ...
- selenium控制超链接在当前标签页中打开或重新打开一个标签页
selenium控制超链接在当前标签页中打开或重新打开一个标签页 在web页面源码中,控制超链接的打开是在当前标签页还是重新打开一个标签页,是由属性target=“_black”进行控制的.如果还有属 ...
- TinyMCE基础配置
选择器配置 插件配置 工具栏配置 菜单配置 皮肤配置 编辑区宽高配置 编辑区样式配置 隐藏状态栏 选择器配置 选择器就是CSS选择器,它告诉TinyMCE哪个元素是可编辑的. 示例: tinymce. ...
- Java集合学习(10):hashcode() 和 equals()方法
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: public native int hashCode(); 根据这个 ...
- mysql日期存储格式int,timestarmp,datetime
int (1).4个字节存储,INT的长度是4个字节,存储空间上比datatime少,int索引存储空间也相对较小,排序和查询效率相对较高一点点 (2)可读性极差,无法直观的看到数据. TIMESTA ...
- EtherNet/IP CIP协议
EtherNet/IP CIP协议 1.EtherNet/IP简述 EtherNet/IP(Ethernet/Indstrial Protocol,以太网/工业协议)是一种基于以太网和TCP/IP技术 ...