PAT甲级——A1078 Hashing
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 ( 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 (≤) and N (≤) 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 -
通过将给定元素值对表长的余数作为在哈希表中的插入位置,如果出现冲突,采用平方探查法解决。平方探查法的具体过程是,假设给定元素值为a,表长为M,插入位置为a%M,假设a%M位置已有元素,即发生冲突,则查找
(a+1^2)%M,(a-1^2)%M,(a+2^2)%M,(a-2^2)%M,⋯⋯,(a+M^2)%M,(a-M^2)%M直至查找到一个可进行插入的位置,否则当查找到(a+M^2)%M,(a-M^2)%M仍然不能插入则该元素插入失败。
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int M, N;
bool isPrime(int num)
{
if (num <= )
return num > ;
if (num % != && num % != )
return false;
int s = (int)sqrt(num);
for (int i = ; i <= s; ++i)
if (num % i == )
return false;
return true;
}
int main()
{
cin >> M >> N;
while (!isPrime(M++));//找到素数
M--;
vector<int>table(M, );
int num, index;
for (int i = ; i < N; ++i)
{
cin >> num;
index = num % M;
if (table[index] > )//存在冲突
{
for (int i = ; i <= M; ++i)
{
index = (num + i * i) % M;
if (table[index] == )
break;
}
}
if(table[index] == )//不存在冲突
{
table[index]++;
cout << index;
}
else
cout << "-";
if (i < N - )
cout << " ";
}
return ;
}
PAT甲级——A1078 Hashing的更多相关文章
- 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 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT甲级1078 Hashing【hash】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
随机推荐
- 织梦自增函数[field:global name=autoindex/]常见用法
看来不少朋友需要不了解这个自增函数的用法,在这里我列举一些常见的写法以及作用. [field:global name=autoindex/] !--普通打印递增的数字-- [field:globa ...
- 了解JNI技术
在线文库系统用到了Jacob,它是基于JNI技术实现的,通过调用MS Office的Com接口实现,我这里对JNI技术进行一个学习. 基本上JNI技术相当于一个代理,因为java的跨平台特性,所以它自 ...
- 转: PHP中this,self,parent的区别
{一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...
- DataList做一个相册,并可以上传图片
1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataLis ...
- Android开发 自定义View_白色圆型涟漪动画View
代码: import android.animation.ValueAnimator; import android.content.Context; import android.graphics. ...
- USACO18FEB Platinum
SlingShot 求数轴上从x到y的最短路( 边长为1),有若干个从xi到yi长度为ti的传送门,每次只能选择其中一个使用. 即求min(|x-y|,min{|a-x|+|b-y|+c}),拆开绝对 ...
- for in循环介绍以及陷阱
大家都知道在JavaScript中提供了两种方式迭代对象: (1)for 循环: (2)for..in循环: 使用for循环进行迭代数组对象,想必大家都已经司空见惯了.但是,使用for.. in循环时 ...
- eclipse查看源码的配置
1.打开eclipse软件,点击window-preference 2.在弹出框中选择java-Installed JRES,选中的默认就行,然后点一下选中的,点击edit 3.弹出框中选择第二个,展 ...
- ros清理日志文件
检查日志文件: rosclean check 清理日志文件: rosclean purge
- Centos7 下修改日期
Centos7 下修改日期 2017年11月19日 19:37:47 harris135 阅读数:2851 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csd ...