PAT甲级——A1145 HashingAverageSearchTime【25】
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 ( 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 1. 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 1.
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
Soulution:
整道题很水的,只不过我一定要注意对于查找不存在的数字的情况,当查到某位置时,此处hash表为空,则表明该数不存在,无需再查找下去
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool isPrime(int x)
{
for (int i = ; i*i <= x; ++i)
if (x%i == )
return false;
return true;
}
int main()
{
int n, m, k, x;
cin >> n >> m >> k;
while (!isPrime(n))++n;
vector<int>v(n, );
while (m--)
{
cin >> x;
bool flag = false;
for (int i = ; i < n && flag == false; ++i)
{
if (v[(x + i * i) % n] == )
{
v[(x + i * i) % n] = x;
flag = true;
}
}
if (flag == false)
printf("%d cannot be inserted.\n", x);
}
double cnt = ;
for (int i = ; i < k; ++i)
{
cin >> x;
for (int j = ; j <= n; ++j)
{
++cnt;
if (v[(x + j * j) % n] == x || v[(x + j * j) % n] == )//==0表示不存在,我就死在这点上了
break;
}
}
printf("%.1f", cnt / k);
return ;
}
PAT甲级——A1145 HashingAverageSearchTime【25】的更多相关文章
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- 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 ...
随机推荐
- 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension
Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...
- ATT&CK实战系列 红队实战(一)————环境搭建
首先感谢红日安全团队分享的靶机实战环境.红队实战系列,主要以真实企业环境为实例搭建一系列靶场,通过练习.视频教程.博客三位一体学习. 靶机下载地址:http://vulnstack.qiyuanxue ...
- 1.Jmeter 快速入门教程(一) - 认识jmeter和google插件
Jmeter是免费开源的性能测试工具( 同时也可以用作功能测试,http协议debug工具 ). 在如今越来越注重知识产权的今天, 公司越来越不愿意冒着巨大的风险去使用盗版的商业性能测试工具. 但如 ...
- Cocos2d Box2D之动态刚体
| 版权声明:本文为博主原创文章,未经博主允许不得转载. b2_dynamicBody 动态物体可以进行全模拟.用户可以用手手动移动动态刚体,也可以由动态刚体自己受力而自运动.动态物体可以和任何物 ...
- python的三种创建字典的方法
#创建一个空字典 empty_dict = dict() print(empty_dict) #用**kwargs可变参数传入关键字创建字典 a = dict(one=,two=,three=) pr ...
- Apache Shiro 会话+缓存+记住我(三)
1.会话管理SessionDao和SessionManager 1)安装Redis 2)依赖 <dependency> <groupId>redis.clients</g ...
- js 监听input 实现数据绑定
<!DOCTYPE html> <html> <head> <script> function checkField(val) { //alert(&q ...
- JAVA 实现Jacob语音播报
准备工作:下载Jar 链接:https://pan.baidu.com/s/1edskJjYrCiefVJ7l3Ul9kQ 提取码:6dg9 ---导入jar 解压jar包,将jacob.ja ...
- 大数据基础环境--jdk1.8环境安装部署
1.环境说明 1.1.机器配置说明 本次集群环境为三台linux系统机器,具体信息如下: 主机名称 IP地址 操作系统 hadoop1 10.0.0.20 CentOS Linux release 7 ...
- 利用PHP和百度ai实现文本以及图片的审核
步骤: 首先打开百度ai 开发平台 注册一个账号: 注册账号,进入控制台 创建自己的应用,获取apikey 和秘钥 进入文档页 文本审核: 图像审核: 代码实例: class Sentive { pr ...