题目信息

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. PAT甲题题解-1078. Hashing (25)-hash散列

    二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...

  4. PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]

    题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 1016. Phone Bills (25)——PAT (Advanced Level) Practise

    题目信息: 1016. Phone Bills (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A l ...

随机推荐

  1. Go语言之进阶篇操作redis

    1.windows安装redis 软件包下载地址: https://github.com/MicrosoftArchive/redis/releases 1.1.安装--->下一步---> ...

  2. CSV 数字转化文本

    最近遇到一个Bug问题,csv 数值转化为文本的问题. 数据如下: 运行效果 如下: 大家看到“01720” 前面的0 没有显示出来.怎样才能显示出来了, 这里的csv文件格式也没有什么问题.后来找到 ...

  3. JavaScript-json数组排序

    排序是开发中不可避免的,最近遇到一个需求需要将JSON数组排序,需求比较简单,实现起来也没什么难度,简单记录下过程: 首先我们需要明白的JavaScript本身的排序是可以传入函数比较的,数组排序如下 ...

  4. iOS开发-Instruments性能调优

    性能是苹果审核的一个很重要的部分,CPU,内存,图形绘制,存储空间和网络性能都是应用的重要的评估和组成部分.不管是作为个人应用开发者还是企业的开发人员,都需要遵循的一个原则是站在用户的角度去思考问题, ...

  5. redis 基本性能测试说明

    http://ghoulich.xninja.org/2016/11/17/how-to-use-redis-benchmark-to-measure-performance/

  6. eclipse中的项目鼠标右键卡死

    1.错误描写叙述 在eclipse中部署了Java Web项目,想在WebContent目录下新建一个目录,鼠标右键时出现eclipse卡死的想象 2.错误原因 (1)插件安装过多 (2)导入的项目过 ...

  7. ifdef ANDROID总是不好用

    在vs的proprocessor definition中定义了ANDRIOD, 但是执行下面的代码的时候,总是说ANDROID is not defined.     #ifdef ANDROID   ...

  8. 浅谈压缩感知(十五):感知矩阵之spark常数

    在压缩感知中,有一些用来评价感知矩阵(非测量矩阵)的指标,如常见的RIP等,除了RIP之外,spark常数也能够用来衡量能否成为合适的感知矩阵. 0.相关概念与符号 1.零空间条件NULL Space ...

  9. Javascript 的模块化编程及加载模块【转载+整理】

    http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 本文内容 引入 模块化 最初写法 对象写法 立即执行函数写法 放大模式 宽放 ...

  10. Docker Inspect

    1.Inspect结果详细信息 docker inspect 7988f914a122 其中7988f914a122是某一容器进程的id { "Id": "7988f91 ...