【PAT甲级 - C++题解】1145 Hashing - Average Search Time
admin
2024-04-11 04:17:04
0

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1145 Hashing - Average Search Time (pintia.cn)
🔑中文翻译:期终成绩
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1145 Hashing - Average Search Time

1638. 哈希 - 平均查找时间 - AcWing题库

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

题意

这道题给定一个表的大小以及元素的个数,我们需要利用二次探测法来计算每个元素在表中的位置,如果无法插入则输出 %d cannot be inserted. 。然后再对给定的数值进行查询,并输出查找的平均时间。需要注意的是,如果无法找到一个数值,则该查找的时间为哈希表长 s1s+1

另外,表的大小必须为质数,如果给定的不是质数,则需要求出大于该值的最小质数当做表长。

关于什么是二次探测法,我在之前的 C++ 实现查找的博客中有详细讲解,传送门如下:

(239条消息) C++实现查找 - 顺序、二分和哈希查找_Pandaconda的博客-CSDN博客_顺序查找c++

思路

具体思路如下:

  1. 先确保哈希表长为质数,如果给定的表长不为质数,则找到大于给定表长的最小质数。
  2. 插入给定的数值,如果无法插入则输出对应语句。
  3. 查询给定的数值,最后输出平均查找时间。

代码

#include
using namespace std;const int N = 10010;
int s, n, m;
int h[N];//判断x是否为质数
bool is_prime(int x)
{if (x == 1)    return false;for (int i = 2; i * i <= x; i++)if (x % i == 0)return false;return true;
}//查找位置的同时计算查询的次数
int find(int x, int& cnt)
{int t = x % s;cnt = 1;for (int i = 0; i < s; i++, cnt++){int k = (t + i * i) % s;if (!h[k] || h[k] == x)   return k;}return -1;
}int main()
{cin >> s >> n >> m;//确保哈希表长度为质数while (!is_prime(s))  s++;//插入元素for (int i = 0; i < n; i++){int x, count;cin >> x;int t = find(x, count);if (t == -1)   printf("%d cannot be inserted.\n", x);else    h[t] = x;}//查询元素int cnt = 0;for (int i = 0; i < m; i++){int x, count;cin >> x;find(x, count);cnt += count;}//输出平均查找时间printf("%.1lf\n", (double)cnt / m);return 0;
}

相关内容

热门资讯

4家银行AIC现身存储巨头股东... 近日,资本市场热度颇高的两家存储巨头长鑫科技集团股份有限公司(以下简称“长鑫科技”)、长江存储控股股...
8元无限续杯、0元看电影、老字... 城市的烟火暖意,藏在亲民的消费场景里,也藏在老地标的新生蜕变中。粤汉码头火车旁新开竹林茶馆,8元就能...
2026年水利工程新趋势,这些... 随着全球气候变化和城市化进程的加速,水利工程在保障水资源供给、改善生态环境以及提升人民生活质量中的作...
原创 发... 这几年,身边越来越多人开始换一种活法:不急着买房,不执着“上车”,反而愿意把钱拿去租一套更舒服、更体...
小红书入场Skill分发,B站... 来源:界面新闻 文丨AI价值官 星野 编辑丨美圻 过去半年,Skill 这个词在AI圈的出现...
2026年福州企业门户网站建设... 本篇将回答的核心问题 在数字化转型加速的2026年,企业门户网站建设应遵循哪些核心评估标准,以确保投...
原创 今... 今日金价:2026年5月22日注意了!黄金或现历史类似回调走势 5月22日,金市又热闹起来了,咱们看...
雷军发布YU7 GT、YU7标... 5月21日,小米人车家全生态新品发布会在北京举办,小米集团创始人、董事长兼CEO雷军正式发布小米YU...
留神峪煤矿瓦斯爆炸事故发布会:... 昨晚,山西留神峪煤矿发生瓦斯爆炸,造成重大人员伤亡。今天,当地召开新闻发布会,现场全体默哀。会上介绍...
原创 修... 修复资产负债表,日本花了几十年。 自上世纪90年代初泡沫经济破裂后,日本陷入了长达三十年的通缩螺...
2026年小红书效果化种草白皮... 2026 年小红书正式迈入种草效果化时代,这是品牌追求预算确定性回报与平台升级为消费决策、用户信任场...
连续18年获“全国文化企业30... 南都讯 记者钟欣5月21日,第二十二届中国(深圳)国际文化产业博览交易会开幕。展会期间,光明日报社和...
荣耀确认IPO未终止!开放员工... 5月22日,荣耀因股改满一年未完成IPO,按约定正式开放员工持股退出通道。据《财闻》报道称,当日16...
易方达蓝筹精选有新变动:增聘2... 《每日经济新闻》记者获悉,继景顺长城、中欧等多家基金公司旗下百亿基金经理产品调整后,易方达基金也迎来...
光储龙头,又翻倍了 去年海外光储赛道最受关注的公司,毫无疑问是阳光电源,市值重回巅峰,风光无限。 但今年一季度业绩突然失...
中企出海报告在静安发布,七成受... 来源:滚动播报 (来源:上观新闻) 昨天,在上海静安举办的澳洲会计师公会出海论坛暨澳洲注册会计师颁...
京蒙协作延链强链 科右中旗牛产... 初夏时节,走进内蒙古华阳牛业科技集团有限公司屠宰加工车间,自动化生产线高效运转。作为京蒙协作产业帮扶...
原创 中... 最近发布了一份有关新一线城市魅力的榜单。榜单按照商业资源聚集度、城市枢纽性、城市人活跃度这五个方面来...
突然,全线跳水!超16万人爆仓 来源:宁波晚报 5月23日,被视作反映市场风险偏好指标的加密货币持续跳水。 截至发稿,比特币大跌3....
基民懵了!说好的科技行情,结果... 每经记者:叶峰 每经编辑:赵云 本周股指冲高回落,沪深两市股票型ETF和跨境型ETF合计净流出729...