博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1078. Hashing (25)-PAT甲级真题
阅读量:4677 次
发布时间:2019-06-09

本文共 3001 字,大约阅读时间需要 10 分钟。

1078. Hashing (25)

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 (<=104) 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 –

#include 
using namespace std;int table[10001]; bool isprim(int n){ if (n == 1) return false; for (int i = 2; i*i<=n; i++) { if (!(n%i)) return false; } return true;}int adjust(int n){ for (int i = n;; i++) { if (isprim(i)) return i; }}int visit[10001];int main(){ int Msize, n; cin >> Msize >> n; int size = adjust(Msize); int temp; for (int i = 0; i < n; i++) { cin >> temp; int j = temp%size; int step = 0; int flag = 0; int flag1 = 0; int key = 0; while (visit[j]) { j = (temp+(step*step))%size; //j = %size; //key = (j + step*step) % size; step++; //flag = 1; if (step > size) { flag1 = 1; break; } } if (flag1 == 1) cout << ' ' << '-'; else { if (i == 0) cout << j; else cout << ' ' << j; visit[j] = 1; } /* if (visit[j] == 0) { visit[j] = 1; if (i == 0) { cout << j; } else cout << ' ' << j; } else { for (int step = 1; step < n; step++) { j = (temp%size + step*step)%size;//注意这里,如果使用j,j是一个迭代变量,而其实这里只是希望做一个初始值,step才是迭代变量。 if (visit[j] == 0) { visit[j] = 1; cout << ' ' << j; flag = 1; break; } } if (!flag) cout << ' ' << '-'; */ }}

这道题并不难,有个小坑,在二次探测时,要注意迭代变量。注释那里只需要赋初值,小心当成迭代变量使用。

转载于:https://www.cnblogs.com/patforjiuzhou/p/7468651.html

你可能感兴趣的文章
Servlet监听器统计在线人数
查看>>
第2章 数字之魅——寻找发帖“水王”
查看>>
eclipse jsp html 格式化 format
查看>>
关于手机端IOS系统微信中虚拟键盘遮挡input输入框问题的解决方案 草稿
查看>>
css3背景、边框、和补丁相关属性 (二)
查看>>
Python--小功能应用
查看>>
别做操之过急的”无效将军”,做实实在在的”日拱一卒”
查看>>
js去除范围内所有标签并显示指定字符串
查看>>
结对项目进度2
查看>>
git + git flow 的简单介绍
查看>>
如果我们想要交换两个数字,就可以使用位运算
查看>>
求给出第 K个 N位二进制数,该二进制数不得有相邻的“1”
查看>>
P1059 明明的随机数【去重排序】
查看>>
HDU 1060 Leftmost Digit【log10/求N^N的最高位数字是多少】
查看>>
tomcat配置文件web.xml与server.xml解析--重要
查看>>
【C语言】《C Primer Plus》递归:以二进制形式输出整数
查看>>
使用框架的——好处
查看>>
如此大量的代码,但每个类里面的代码却不显得特别多,原因。。。。。。。。。。。。...
查看>>
C#特征备忘
查看>>
Java 面向对象 之 final 关键字
查看>>