【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;
}

相关内容

热门资讯

成分股集体上涨,港股通汽车ET... 截至2月24日14点10分,上证指数涨0.89%,深证成指涨1.41%,创业板指涨1.11%。ETF...
韩国股指再刷新历史新高!2月最... 来源:财联社 财联社2月23日讯(编辑 马兰)周一,受益于美国对等关税政策无效、人工智能热潮继续刺激...
影石开工红包:每人100,到岗... 2月24日消息,影石为员工安排开工红包,每人100,当天到岗就有。 据介绍,该红包由部门leader...
太疯狂!“现在至少要10万”,... 2月24日早盘,贵金属盘初直线飙升。现货黄金突破5240美元/盎司,触及三周高点,纽约期金突破526...
别盲目跟风!华工科技赶单忙到疯... 最近华工科技涨得有点猛,身边不少人问我是不是又在炒概念。其实吧,这次真不是瞎炒,背后有实打实的订单撑...
韩广岳再辞任董事长职务,李学军... 近日,中邮资管发布公告称,因年龄原因,韩广岳正式向董事会提交辞去公司董事长、董事会投资决策委员会主任...
2026年中国智能舒缓穿戴设备... 华经产业研究院为助力企业、科研、投资机构等单位了解智能舒缓穿戴设备行业发展态势及未来趋势,特重磅推出...
茅台带动春节白酒价格触底止跌,... 虽然业内对2026年春节白酒市场有着“最冷”的担忧,但记者了解到,在茅台的带动下,尽管白酒市场整体需...
广东省情中心:广东生产性服务业... 2月24日,春节后首个工作日,广东连续第四年召开全省高质量发展大会,今年聚焦“制造业与服务业协同发展...
2022三文鱼行业及市场洞察报... 今天分享的是:2022三文鱼行业及市场洞察报告 报告共计:32页 三文鱼作为一种备受青睐的高端水产,...
AI芯片新突破 12只概念股获... 来源:证券时报e公司 人民财讯2月24日电,据科技日报2月23日报道,北京大学电子学院研究员邱晨光团...
原创 不... 特朗普突然改口,全球关税在短时间内再度上调5%,引发市场与舆论的广泛关注。有分析指出,在这一轮博弈中...
湖南三线小城春节消费火爆,商场... 常德街道。胡雅文/摄 本报(chinatimes.net.cn)记者胡雅文 常德报道 马年春节,常德...
小红书负面笔记处理|品牌舆情优... 在当下的社交媒体版图中,小红书早已超越单纯的“种草社区”定位,成长为月活跃用户突破3亿、每日新增笔记...
原创 俄... 最近,国际金融圈发生了一件让许多观察者都感到摸不着头脑的大事——俄罗斯正在源源不断地将黄金运往东方,...
知名食品上市公司实控人陈飞龙去... 2月23日晚间,南侨食品(SH605339)公告称,公司董事会收到公司实控人之一陈飞龙家属通知,陈飞...
高开高走!马年首日三大股指半日... 金融投资报记者 林珂 马年首个交易日大盘如期高开高走,早盘三大股指均出现超过1%的涨幅。 上证指数...
千亿市值巨头,涨停 马年首个交易日,A股主要指数早盘集体上涨。截至午盘,沪指涨1.17%,深证成指涨1.82%,创业板指...
匈牙利外长:决定向谁买能源是主... 据央视新闻报道,欧盟成员国外长2月23日未能就第20轮对俄罗斯制裁方案达成一致。匈牙利外交与对外经济...
IC外汇平台:日本经济长期疲软... 根据国际清算银行数据,今年1月日元实际有效汇率指数降至67.73,创1973年以来最低。该指数反映日...