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 ...
随机推荐
- 【原创】复制知乎“禁止转载”的内容做笔记 - 基于oncopy监听器的简单解决方案
原理:移除所有oncopy的监听器. 使用: 新建书签,地址设为: javascript: getEventListeners(document).copy.forEach(({listener}) ...
- md5加密报错解决方法(TypeError: Unicode-objects must be encoded before hashing)
update()必须指定要加密的字符串的字符编码
- hdfs 创建一个新用户
需要给第三方提供hdfs用户,和上传文件的权限 1.需要先在linux 上创建一个普通用户: hn,并修改密码 sudo -u hdfs hadoop fs -mkdir /user/用户名 sudo ...
- Jquery查找界面Html元素的方法(持续更新)
1. 根据id来获取:$("#id") 2.根据class来获取:$(".class") 3.根据name来获取:$("[name=??]" ...
- 使用 lombok 简化代码
使用前的准备 1.Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现这一目的. <1>添加 ...
- 进程启动到别的session下(作用)
https://blog.csdn.net/lostwifi/article/details/76472868 WTSGetActiveConsoleSessionId WTSEnumerateSes ...
- [js测试]JavaScript Web Quiz By davidshariff
Question1 var foo = function foo() { console.log(foo === foo); }; foo(); 输出是"true",因为foo就指 ...
- event(1)
event event(事件流)是 window对象的一个属性 在JS中事件有2种类型 一种是冒泡类型 一种是捕获类型 冒泡类型最先是在IE中出现,而捕获类型最先在标准的DOM中出现,不过最终IE得胜 ...
- java基础学习笔记四(异常)
Java中的异常 Exception 如图可以看出所有的异常跟错误都继承与Throwable类,也就是说所有的异常都是一个对象. 从大体来分异常为两块: 1.error---错误 : 是指程序无法处理 ...
- Vue学习笔记【27】——Vue路由(设置路由)
设置路由高亮 css: .router-link-active, /* vue-router*/ .myactive { color: red; font-weigh ...