PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). 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 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 104. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 105.
Output Specification:
For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted.
where X
is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.
Sample Input:
4 5 4
10 6 4 15 11
11 4 15 2
Sample Output:
15 cannot be inserted.
2.8
#include <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=;
int a[maxn];
int msize,n,m,k;
vector<int> v;
bool isprime(int i){
if(i== || i==)return false;
if(i== || i==)return true;
for(int j=;j*j<=i;j++){
if(i%j==)return false;
}
return true;
}
int main(){
fill(a,a+maxn,-);
scanf("%d %d %d",&msize,&n,&m);
while(!isprime(msize)){
msize++;
}
for(int i=;i<n;i++){
int tmp;
scanf("%d",&tmp);
int j;
for(j=;j<msize;j++){
if(a[(tmp+j*j)%msize]==-){
a[(tmp+j*j)%msize]=tmp;
break;
}
}
if(j==msize){
printf("%d cannot be inserted.\n",tmp);
}
}
float cnt=;
for(int i=;i<m;i++){
int tmp;
scanf("%d",&tmp);
int j;
cnt++;
for(j=;j<msize;j++){
if(a[(tmp+j*j)%msize]==tmp || a[(tmp+j*j)%msize]==-){
break;
}
cnt++;
}
}
printf("%.1f",cnt/m);
}
注意点:hash 散列的平方探查法,题目只要求加法。正常是加减都有,规则为(key+k*k)%tsize,当k在0-tsize内都不满足条件时说明该元素无法插入。减法出现小于0时,((key-k*k)%tsize+tsize)%tsize,即不断加tsize的第一个非负数。
第二点,题目的计算平均查询次数,总感觉有问题,当这个数插不进去时要多加一次,不知道为什么。
PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法的更多相关文章
- 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 1145 Hashing - Average Search Time [hash][难]
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...
- PAT Advanced 1145 Hashing – Average Search Time (25) [哈希映射,哈希表,平⽅探测法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- PAT 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 1145. Hashing - Average Search Time (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT Basic 1043 输出PATest (20分)[Hash散列]
题目 给定⼀个⻓度不超过10000的.仅由英⽂字⺟构成的字符串.请将字符重新调整顺序,按"PATestPATest-."这样的顺序输出,并忽略其它字符.当然,六种字符的个数不⼀定是 ...
- PAT_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
随机推荐
- 【Dubbo&&Zookeeper】6、 给dubbo接口添加白名单——dubbo Filter的使用
在开发中,有时候需要限制访问的权限,白名单就是一种方法.对于Java Web应用,Spring的拦截器可以拦截Web接口的调用:而对于dubbo接口,Spring的拦截器就不管用了. dubbo提供了 ...
- sublime3安装ctags追踪插件
sublime3经常要用到函数追踪插件,怎做的?下面看安装步骤: 1.安装package control 按快捷键 ctrl+shift+p 2.安装搜索 ctags插件 3.下载ctags可执行程序 ...
- Oracle总结二
1 where子句 Where子句的作用:用where子句来指定查询条件 1.1 用法示例 select * from emp where deptno=10; select * from emp w ...
- 一个Web页面的问题分析
几个月之前我接到一个新的开发任务,要在一个旧的Web页面上面增添一些新的功能.在开发的过程中发现旧的代码中有很多常见的不合适的写法,结合这些问题,如何写出更好的,更规范的,更可维护的代码,就是这篇文章 ...
- Oracle 11g即时客户端在windows下的配置
Oracle 11g即时客户端在windows下的配置 by:授客QQ:1033553122 instantclient-basic-nt-11.2.0.3.0.zip客户端压缩包为例 步骤 1. 假 ...
- linux上部署engineercms、docker和onlyoffice实现文档协作
等了好久,这次终于下决心在局域网部署了linux系统,并安装docker和load了onlyoffice,利用engineercms进行资料管理和文档协作. 我整理了完整文档,见我的网盘. engin ...
- asp.net core中IHttpContextAccessor和HttpContextAccessor的妙用
分享一篇文章,关于asp.net core中httpcontext的拓展. 现在,试图围绕HttpContext.Current构建你的代码真的不是一个好主意,但是我想如果你正在迁移一个企业类型的应用 ...
- Redis系列(一):Redis的简介与安装
原文链接(转载请注明出处):Redis系列(一):Redis的简介与安装 什么是 Redis Redis 是一个使用ANSI C 编写的开源.支持网络协议.基于内存.可选持久性的键值对数据库,它是一个 ...
- vs下开发windows服务程序
一. VS2012下开发Windows服务 1. 打开VS2012,新建项目,选择Windows服务,此处我以开发一个定时自动发送邮件的服务来做介绍,如下图: 2. 创建好后,编译器会自动创建一些文件 ...
- 如何创建和还原SQL Server 2000数据库?
说明:这篇文章是几年前我发布在网易博客当中的原创文章,但由于网易博客现在要停止运营了,所以我就把这篇文章搬了过来,虽然现如今SQL Server 2000软件早已经过时了,但仍然有一部分人在使用它,尤 ...