1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息
1078. Hashing (25)
时间限制100 ms
内存限制65536 kB
代码长度限制16000 B
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 “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 two positive numbers: MSize (<=10^4) and N (<=MSize) 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 -
解题思路
题目默认你已掌握平方二次探測,有多坑逼。,,,
AC代码
#include <cstdio>
#include <cmath>
bool a[20005];
bool prime(int a){
if (1 == a) return false;
if (2 == a || 3 == a) return true;
int t = (int)sqrt(a);
for (int i = 2; i <= t; ++i){
if (a%i == 0){
return false;
}
}
return true;
}
int main()
{
int ms, n, ts, t, i, j;
scanf("%d%d", &ms, &n);
ts = ms;
while (!prime(ts)){
++ts;
}
for (i = 0; i < n; ++i){
scanf("%d", &t);
for (j = 0; j < n; ++j){
if (!a[(t + j*j) % ts]){
a[(t + j*j) % ts] = true;
printf("%s%d", i ?
" ":"", (t + j*j) % ts);
break;
}
}
int loc = t % ts;
if (j == n){
printf("%s-", i ? " ":"");
}
}
return 0;
}
1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise的更多相关文章
- 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise
题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...
- 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise
题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...
- PAT甲题题解-1078. Hashing (25)-hash散列
二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...
- 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 (Advanced Level) Practise - 1093. Count PAT's (25)
http://www.patest.cn/contests/pat-a-practise/1093 The string APPAPT contains two PAT's as substrings ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- PAT (Advanced Level) Practise - 1097. Deduplication on a Linked List (25)
http://www.patest.cn/contests/pat-a-practise/1097 Given a singly linked list L with integer keys, yo ...
- 1016. Phone Bills (25)——PAT (Advanced Level) Practise
题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...
随机推荐
- Java信号量Semaphore
Semaphore Semaphore分为单值和多值两种,前者只能被一个线程获得,后者可以被若干个线程获得. Semaphore实现的功能就类似厕所有5个坑,假如有10个人要上厕所,那么同时只能有多少 ...
- Android -- startActivityForResult和setResult
startActivityForResult与startActivity的不同之处 startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivit ...
- mahout0.7 示例运行纪实
http://1992mrwang.blog.51cto.com/3265935/1205282 运行太不容易了 所以要记下来以免后面忘记了 首先是数据 vim testdata.txt 第一列为Us ...
- 【转】BFC是什么
原文:https://www.cnblogs.com/mlw1814011067/p/10397999.html ------------------------------------------- ...
- Mongoose的分页功能
来自: https://github.com/edwardhotchkiss/mongoose-paginate 拷贝如下: Note: This plugin will only work wi ...
- 【Nodejs】外研社一年级起各年级英语音频下载(缺456年级上)
在 https://news.21cnjy.com/A/130/235/V729768.shtml 有各年级英语音频下载,用爬虫把能下的都下了,除了四五六年级上册的. 爬虫 http://www.cn ...
- (算法)等概率选出m个整数
题目: 从大小为n的整数数组A中随机选出m个整数,要求每个元素被选中的概率相同. 思路: n选m,等概率情况下,每个数被选中的概率为m/n. 方法: 初始化:从A中选择前m个元素作为初始数组: 随机选 ...
- java class 文件
class 文件是什么 1).是对一个java类或一个java接口的全面描述:2).是对java程序二进制文件格式的精确定义:3).一个class文件中只能包含一个类或接口:4).作为程序传给jvm的 ...
- Discuz常见小问题-如何取消登陆发帖验证码
1 正常情况下,用户点击登录之后,需要填写验证码 2 进入后台,点击防灌水,验证设置,然后下面的各个选项可以设置是否启用验证码.
- 坑人无数的Redis面试题
https://baijiahao.baidu.com/s?id=1588454565071211950&wfr=spider&for=pc 坑人无数的Redis面试题