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部分.语言是 ...
随机推荐
- C++类成员变量多用指针不用对象
如A类的成员变量含有B类的对象,那么每个A类对象产生或拷贝都要产生一次B类对象的构造或者拷贝,对象占的空间比较大,对象拷贝比较消耗内存. 如果换成B类的指针,A类对象拷贝,也只会产生4个字节或者8个字 ...
- java编程——数据的加法
设计思想: 第一步:从键盘上输入一定个数的整数. 第二步:因为在main()方法中的参数是String类型的,所以第一步中输入的其实是字符类型,在这一步要把String转化成int. 第三步:初始化s ...
- Python全栈开发:django网络框架(一)
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- CSIC_716_20191105【数字、字符串、列表】
python数据类型及其内置方法 一.整型:主要用于数学运算 其他进制----->转十进制 """ 其他进制转换为十进制 通过int('x进制数',x)实现 &qu ...
- Java类加载器浅述
jdk默认提供了三种类加载器: 1.Bootstrap ClassLoader(引导类加载器): 将<JAVA_HOME>\lib目录下的类库加载到虚拟机内存中,用来加载java的核心库, ...
- Postgraduate
https://account.chsi.com.cn/passport/login?entrytype=yzgr&service=https%3A%2F%2Fyz.chsi.com.cn%2 ...
- 校园商铺-4店铺注册功能模块-8店铺注册之Controller层的改造
不合理的地方: 1. 并不需要将InputStream转换成File类型,直接将InputStream传进入交给CommonsMultipartfile去处理就可以了 如果做这样的转换,每次都需要生成 ...
- js日常总结
1.html如何引入css和js文件 css:<link rel="stylesheet" href="css/index1.css(这是我的文件的地址)" ...
- golang中使用gorm连接mysql操作
一.代码 package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/go- ...
- hdu多校第三场 1007 (hdu6609) Find the answer 线段树
题意: 给定一组数,共n个,第i次把第i个数扔进来,要求你删掉前i-1个数中的一些(不许删掉刚加进来这个数),使得前i个数相加的和小于m.问你对于每个i,最少需要删掉几个数字. 题解: 肯定是优先删大 ...