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 ...
随机推荐
- 利用单例模式设计数据库连接Model类
之前在<[php]利用php的构造函数与析构函数编写Mysql数据库查询类>(点击打开链接)写过的Mysql数据库查询类还不够完美,利用<[Java]单例模式>(点击打开链接) ...
- 2.2.3 TableLayout(表格布局)
3.如何确定行数与列数 ①如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!! ②如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面! ③ ...
- 网站横幅切换jquery 插件
最近做一个web项目,站点首页需要像 百度统计 页面类似的切换横幅,有兴趣的可以先看看它的效果,这样就比较容易理解为什么我单独做个插件.其实类似的 jquery 插件网上类似的多的去了,随便网上下了两 ...
- shiro实战系列(一)之入门实战
一.什么是shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密. Apache Shiro 的首要目标是易于使用和理解.安全有 ...
- SpringMVC之ajax+select下拉框交互常用方式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python2.7-copy_reg
copy_reg 模块,提供了在 pickle 或是 copy 特定对象时,可以运行一个指定的函数,作为对象的构造器 模块方法: copy_reg.constructor(object):声明一个可调 ...
- 在ASP.NET Core上实施每个租户策略的数据库
在ASP.NET Core上实施每个租户策略的数据库 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://g ...
- scp 免密登录ssh-copy-id 如何使用非22端口 + rsync 同步远程机器的文件到本地
其中:id_rsa为私钥文件,id_rsa.pub为公钥文件 ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 2122 root@IP " 我的:s ...
- Python3.2-re模块之常用正则记录
python的re模块是个很好的模块,这里简单记录下自己编写的几个有用的正则: 1:邮箱匹配: gReMailbox = re.compile(r'([\w\.\-+]+@[\w\-]+(?:\.[\ ...
- mysql的常用优化知识
索引类型:主键索引,唯一索引,联合索引,普通索引,全文索引 建立索引: create index index_name on table(field_name); 删除索引: drop index i ...