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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  4. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  5. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  6. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  7. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  8. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  9. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

随机推荐

  1. day 67 Django的view 与路由

    一.Django中的视图 CBV和FBV 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的. url(r'^add_publisher/',views.AddPublis ...

  2. TP框架实现文件的下载(主要解决文件存在中文文件名的问题)

    namespace Home\Controller; use Think\Controller; use Org\Net\Http; class IndexController extends Con ...

  3. [FW]使用kprobes查看内核内部信息

    使用printk打印变量等方法,是调试内核的有效方法之一,但是这种方法必须重新构建并用新内核启动,调试效率比较低.以内核模块的方式使用kprobes.jprobes,就可以在任意地址插入侦测器,执行包 ...

  4. maven私服的项目使用配置

    环境:  eclipse .maven.nexus. 1.配置setting.xml文件 1.1.配置本地仓库位置:文件中,存在节点 “localRepository”,默认是注释,也就是本地仓库使用 ...

  5. 【转】java中JVM的原理

    转载自https://blog.csdn.net/witsmakemen/article/details/28600127/ 一.java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Ja ...

  6. 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现

    题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...

  7. Oracle修改数据文件路径

    更改Oracle数据文件名及数据文件存放路径 SQL> select * from v$dbfile;      FILE# NAME---------- ------------------- ...

  8. php递归无限分类、根据子类获取所有顶类

    //递归无限分类树 public static function diGui($data, $pid) { $arr = collect([]); if (empty($data)) { return ...

  9. &与&&、|与||的区别

    &和&& 相同之处: &和&&都表示:符号两端必须同时为真,最后的结果才为真:其中一端为假,则最后的结果为假 不同之处: &:左端为假,还需要继 ...

  10. Halo(四)

    BeanWrapper 接口 操作属性 package org.springframework.beans; BeanWrapper bw = new BeanWrapperImpl(beanObje ...