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 ...
随机推荐
- 小米手机_adb安装apk报错”Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]“
问题: adb安装apk至小米手机时,安装失败,报错提示“Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]”,如下图 ...
- do_mmap解读
1: unsigned long do_mmap_pgoff(struct file *file, unsigned long addr, 2: unsigned long len, unsigned ...
- Java迭代器模式
迭代器模式是Java和.Net编程环境中非常常用的设计模式.此模式用于以顺序方式访问集合对象的元素,而不需要知道其底层表示. 迭代器模式属于行为模式类别. 实现实例 在这个实例中,将创建一个Itera ...
- 【问题解决方案】anaconda-python在cmd-pip安装requests后依然提示No module named requests
参考: 知乎回答:python的requests安装后idle依然提示No module named requests? 环境: win7-64位 anaconda3-Python3.7 & ...
- Vue内敛模板
在学习<Vue实战>一书时,学习到组件高级应用-内联模板这一小节时,照着书上的例子敲了一遍,发现未达到预期,切报错. 书上源代码如下: <!DOCTYPE html> < ...
- Ubuntu中安装jenkins+docker,实现项目部署
本人对于linux系统是个小白,恰逢公司新框架需要docker+jenkins部署项目,所以通过同事口述+一顿乱查,终于实现在虚拟机上搭建的ubuntu系统中 实现jenkins +docker 自动 ...
- java -jar 中文乱码
java -Dfile.encoding=utf-8 -jar demo.jar 添加编码即可
- 二、kubernetes搭建集群
一.准备工作 主要内容 .环境准备(2主机) .安装流程 .问题分析 .总结 环境配置(2主机) 系统:CentOS 7.3 x64 网络:局域网(VPC) 主机: master:172.16.0.1 ...
- 基于 Scrapy-redis 两种形式的分布式爬虫
基于 Scrapy-redis 两种形式的分布式爬虫 .caret, .dropup > .btn > .caret { border-top-color: #000 !important ...
- 浏览器CSS兼容
一.<important 在IE6及FF中的使用>.box1 {width:150px !important;} .box1 {width:250px;} !important是说这个设置 ...