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

1078. Hashing (25)-PAT甲级真题相关推荐

  1. 1020. Tree Traversals (25) PAT甲级真题

    之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后 ...

  2. 1040. Longest Symmetric String (25)-PAT甲级真题

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  3. 1006. Sign In and Sign Out (25)-PAT甲级真题

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  4. 1085. Perfect Sequence (25)-PAT甲级真题

    Given a sequence of positive integers and another positive integer p. The sequence is said to be a & ...

  5. 1121. Damn Single (25)-PAT甲级真题

    "Damn Single (单身狗)" is the Chinese nickname for someone who is being single. You are suppo ...

  6. 1090. Highest Price in Supply Chain (25)-PAT甲级真题

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  7. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  8. 1114. Family Property (25)-PAT甲级真题(并查集)

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  9. 1102. Invert a Binary Tree (25)-PAT甲级真题

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

最新文章

  1. IPv6扩展头部 (一) 扩展头部格式、类型与扩展选项
  2. SAP HANA里执行SQL语句的两种方式
  3. 基础算法 —— 贪心算法
  4. 第六章 实验报告(函数与宏定义)
  5. 探讨:软件厂商Kaseya事件是不是软件供应链攻击?
  6. 牛客小白月赛5求阶乘末尾有多少个0
  7. 把PDF/AZW/EPUB转成图片
  8. 2个路由器串联做交换机共享网络的方法
  9. ORAN C平面 Section Type 7
  10. 苹果Mac电脑L2TP连接公司内部网络失败解决方案
  11. 在VMware vCenter中使用企业CA或第三方CA替换VMCA
  12. 数据分析师需要学什么?数据分析师需要掌握什么技能呢?
  13. Acrobat Pro DC 教程:了解 Acrobat Pro DC 界面
  14. iPad 3 即将发布,网传价格,IPD2降价50$ 新功能猜测
  15. 区块链+社群经济是什么样子?让「牛顿」的NewMall告诉你
  16. python批量下载文件教程_超简单超详细python小文件、大文件、批量下载教程
  17. python plt 绘图详解(plt.版本)
  18. 一个支持钉钉远程定时打卡脚本
  19. c语言飞机买票系统,C语言实现飞机订票系统
  20. Phpword+thinkphp5

热门文章

  1. 小智机器人有初中课程吗_征战记大写的优秀!2020世界机器人大赛,看这些奖项收入囊中!...
  2. linux -bash: ./startup.sh: /bin/sh^M: 坏的解释器: 没有那个文件或目录
  3. Git学习小记之分支原理
  4. Java 内存溢出(java.lang.OutOfMemoryError)解决
  5. spring cloud超时时间设置
  6. 内核管理 之 内核管理概述
  7. 百度云BaaS体系揭秘,突破共识机制、单机计算和串行处理三大瓶颈
  8. SaaS 公司如何应对 On-Call 挑战?
  9. 创建一个优质可用的Hyper-V虚拟机模板
  10. Xamarin只言片语3——Xamarin.Android下支付宝(Alipay SDK)使用