PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. 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 two positive numbers: MSize (≤) and N (≤) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.
Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e4 + 10;
int N, M;
int num[maxn], prime[maxn];
int vis[maxn] = {0}; bool isprime(int x) {
if(x == 2) return true;
if(x <= 1) return false; for(int i = 2; i * i <= x; i ++) {
if(x % i == 0) return false;
}
return true;
} void Hash(int x) {
for(int i = 0; i < N; i ++) {
int cnt = (x + i * i) % M;
if(vis[cnt] == 0) {
vis[cnt] = 1;
printf("%d", cnt);
return ;
}
}
printf("-");
} int main() {
/*memset(prime, 1, sizeof(prime));
for(int i = 2; i * i <= maxn; i ++) {
for(int j = 2; j * i < maxn; j ++)
prime[j * i] = 0;
}*/ scanf("%d%d", &M, &N);
for(int i = 0; i < N; i ++)
scanf("%d", &num[i]); while(!isprime(M)) M ++; // mp.clear();
for(int i = 0; i < N; i ++) {
Hash(num[i]);
printf("%s", i != N - 1 ? " " : "");
}
return 0;
}
二次方探查法 想当一个盲流子
PAT 甲级 1078 Hashing的更多相关文章
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT甲级1078 Hashing【hash】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...
- PAT 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT甲级——A1078 Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- PAT 1078 Hashing[一般][二次探查法]
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
随机推荐
- [POI2007]MEG-Megalopolis
传送门:嘟嘟嘟 第一反应是树链剖分,但是太长懒得写,然后就想出了一个很不错的做法. 想一下,如果我们改一条边,那么影响的只有他的子树,只要先搞一个dfs序,为什么搞出这个呢?因为有一个性质:一个节点的 ...
- WorldWind源码剖析系列:缓冲类Cache
缓冲类Cache主要用于在最小的限制条件下保存从远程服务器通过网络下载下来的地理空间数据,以便当用户处于离线状态时能够使用这些已经缓冲好的数据.Google Earth也采用类似机制处理用户离线浏览漫 ...
- java.lang.NoClassDefFoundError: org.androidpn.client.PersistentConnectionListener
在运行AndroidpnClient项目时出现了java.lang.NoClassDefFoundError: org.androidpn.client.PersistentConnectionLis ...
- NLB网路负载均衡管理器详解(转载)
序言 在上一篇配置iis负载均衡中我们使用啦微软的ARR,我在那篇文章也中提到了网站的高可用性,但是ARR只能做请求入口的消息分发服务,这样如果我们的消息分发服务器给down掉啦,那么做再多的应用服务 ...
- MySQL ERROR 1054 的问题
MySQL 1054错误的情况 在用命令插入数据时提示 1054错误的问题: 这种情况下的解决方法: 是因为引号的问题 ‘ ’ . 字符串应该加上引号,解决问题.如下:
- 动画:view从点逐渐变大(放大效果)
-(void) animationAlert:(UIView *)view { CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation ani ...
- net 表格控件
一个开源的表格控件,界面像Excel,看来很好,有机会在项目中使用:ReoGrid https://reogrid.net/
- java 对象是可以判空的
比如这里存xml,这里判断了一下element是否为空,来避免空指针异常,推荐用guava的optional判空
- 一个Python开源项目-哈勃沙箱源码剖析(下)
前言 在上一篇中,我们讲解了哈勃沙箱的技术点,详细分析了静态检测和动态检测的流程.本篇接着对动态检测的关键技术点进行分析,包括strace,sysdig,volatility.volatility的介 ...
- python中类中属性和方法的具体定义方法和使用
1. Python中类中特性分成属性和方法 属性和方法都分为私有和公有的,私有的只可以在本类中使用外部是无法访问的 2. 定义属性(成员变量)的语法格式(公有属性/私有属性) class 类名: de ...