PAT 甲级 1078 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 ( 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 -
题意:
输入msize和N,如果msize不是素数的话就把msize变为比当前值大的最小素数,采用平方探测方法,求插入哈希表后元素的序号
题解:
平方探测方法
二次探测——
其中,h是哈希寻址函数,key是要存储的值,M是哈希表的大小,一般使用素数可以达到一个较高的效率。
AC代码 :
#include<bits/stdc++.h>
using namespace std;
int m,n;
int a[];
bool prime(int x){
if(x<=) return false;
for(int i=;i*i<=x;i++){
if(x%i==) return false;
}
return true;
}
int main(){
cin>>m>>n;
for(int i=;i<;i++) a[i]=-;
while(!prime(m)) m++;
int x;
for(int i=;i<=n;i++){
cin>>x;
int f=;
for(int j=;j<m;j++){
int y=(x+j*j)%m;//平方探测
if(a[y]==- || a[y]==x){
cout<<y;
a[y]=x;
f=;
break;
}
}
if(!f) cout<<"-";
if(i!=n) cout<<" ";
}
return ;
}
PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)的更多相关文章
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- PAT 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
- PAT甲级1078 Hashing【hash】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
随机推荐
- Linux操作系统之grub加密实战案例
Linux操作系统之grub加密实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为grub设置明文密码案例 1>.修改"/boot/grub/grub. ...
- Cloudera Certified Associate Administrator案例之Test篇
Cloudera Certified Associate Administrator案例之Test篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备工作(将CM升级到&qu ...
- MySQL安装-glibc方式安装
MySQL安装-glibc方式安装 版本说明:这里安装版本为MySQL-5.7系列的 mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz 步骤 下载软件包 官网下载 ...
- 《TensorFlow2深度学习》学习笔记(二)手动搭建并测试简单神经网络(附mnist.npz下载方式)
本实验使用了mnist.npz数据集,可以使用在线方式导入,但是我在下载过程中老是因为网络原因被打断,因此使用离线方式导入,离线包已传至github方便大家下载: https://github.com ...
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- CentOS7.5安装SVN和可视化管理工具iF.SVNAdmin
一.安装Apache和PHP 由于iF.SVNAdmin使用php写的,因此我们需要安装php yum install httpd php 二.安装SVN服务器(其中,mod_dav_svn是Apac ...
- 与你一起学习MS Project——高级篇:Project高级应用
我们再来看Project的一些高级应用. 一.设置任务依赖性的几种方法 首先是设置任务依赖性的几种方法,这里介绍三种方法. 方法一:选中两个需要建立依赖型的任务.选中用 ctrl 鼠标左键 的方式即可 ...
- LeetCode 1000. Minimum Cost to Merge Stones
原题链接在这里:https://leetcode.com/problems/minimum-cost-to-merge-stones/ 题目: There are N piles of stones ...
- Can't generate API documentation in l5-swagger
Can't generate API documentation in l5-swagger Ask Question Asked 3 months ago Active 1 month ago Vi ...
- jsp解决大文件断点续传
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...