首页 > 编程语言 >PAT_Advanced Level_1078 Hashing (25分)(C++_Hush_平方探测法)

PAT_Advanced Level_1078 Hashing (25分)(C++_Hush_平方探测法)

时间:2023-06-20 10:01:30浏览次数:43  
标签:25 PAT 1078 int msize numbers table line size


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 -

Process

  • Step 1:改变哈希表的长度;
  • Step 2:使用平方探测法插入元素;
    Quadratic probing(平方探测):PAT_Advanced Level_1078 Hashing (25分)(C++_Hush_平方探测法)_c++

Code

#include<bits/stdc++.h>
using namespace std;
int m[11000], msize, n;
bool isPrime(int a)
{
	if (a == 1)
		return false;
	else
		for (int i = 2; i * i <= a; i++)
			if (!(a % i))
				return false;
	return true;
}
int main()
{
	memset(m, 0, sizeof(m));
	cin >> msize >> n;
	while (!isPrime(msize))
		msize++;
	for (int i = 0; i < n; i++)
	{
		int temp, flag = 0;
		cin >> temp;
		for (int j = 0; j < n; j++)
		{
			int d = (temp + j * j) % msize;
			if (m[d] == 0)
			{
				m[d] = 1;
				cout << d << (i == n - 1 ? "\n" : " ");
				flag++;
				break;
			}
		}
		if(!flag)
			cout << "-" << (i == n - 1 ? "\n" : " ");
	}
	return 0;
}


标签:25,PAT,1078,int,msize,numbers,table,line,size
From: https://blog.51cto.com/u_16165815/6520514

相关文章

  • nginx 1.25. 1 发布
    nginx1.25.1有一个很不错的特性,就是支持了http2指令,以前这个指令主要是也listen配置使用的(ssl+http2场景)独立指令之后就有了很方便的功能了,比如有些业务希望使用http0.9-1.1协议,有些需要使用http2,当然目前也是支持了http3的,可以做到分离,以前版本存在一个问题就是开启了......
  • 阶段性知识总结解释版【Day01-Day25】
    day021.什么是编程和编程语言编程 是指使用计算机语言编写计算机程序的过程。编程语言 是一种用于编写计算机程序的形式化语言,它可以被解释器或编译器转换成机器码以便计算机执行。编程语言包括C、Java、Python、JavaScript、PHP等。2.计算机五大组成部分,分别阐释一......
  • 阶段性知识总结习题版【Day01-Day25】
    day02什么是编程和编程语言计算机五大组成部分,分别阐释一下各自特点计算机三大核心硬件,各自的特点常见的操作系统day03计算机存储数据的单位有哪些,之间的单位换算是怎样的编程语言的发展史,分别有什么特点编程语言的分类python解释器的版本有哪些,推荐使用的版本是哪个......
  • 包含目录,库目录,附加包含目录,附加库目录,附加依赖项,系统Path变量
    https://blog.csdn.net/qq_41607336/article/details/98225543?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-1-98225543-blog-103292393.235^v38^pc_relevant_sort_base2&depth_1-utm_source=distribute.pc_......
  • RT-THREAD的SFUD驱动简介基于W25Q128
    SFUD简介SFUD是一款开源的串行SPIFlash通用驱动库。详细介绍可查看官方说明,作为一个通用的中间套件,帮用户屏蔽了底层的FLASH操作,也方便用户使用不同的FLASH时进行移植。只需要配置好SPI就可以完成驱动的移植。FLASH特点FLASH写的时候,只能从1写到0,而不能从0写到1。因此写之......
  • 智能佳—LoCoBot WX250 6自由度 (用于科研与教学的ROS智能车)
    LoCoBot是用于映射、导航和操纵(可选)等ROS研究的智能车,研究人员、教育工作者和学生都可以使用LoCoBot专注于高级代码的开发,而不是专注硬件和构建低级代码。通过开放的源代码软件、完整的ROS映射和导航包以及模块化的开放源代码PythonAPI,LoCoBot上的开发得以简化,用户仅需10行代码......
  • POSTGRESQL 高可用 Patroni VS Repmgr 到底哪家强(1)
    在众多postgresql高可用模式中,主要的参与者有两位,PatroniVSrepmgr基于这二者的功能优点以及缺点相信大部分人都不是太明确,下面将根据两篇翻译的文字合并,来对两个高可用的程序来做一个比较,consandpros。 1 Repmgr是一款开源的基于postgres复制基础上的高可用软件,他基于2......
  • 从3000ms到25ms!看看人家的接口优化技巧,确实很优雅!!
    批处理避免多次IO异步处理空间换时间使用缓存预处理预计算池化思想数据库连接池,线程池。避免重复创建与销毁。优化程序结构程序经过多次迭代,多人维护开发情况下,会出现一些重复操作等等。串行改并行索引加索引,排除索引失效场景避免大事务......
  • Error creating bean with name 'sqlSessionFactory' defined in class path resource
    项目启动报错原因分析背景:system模块一个月未重启过,今天重启报数据源问题原因:这里报错的原因是数据源配置问题解决:数据源配置在nacos中,拿该模块的nacos数据源配置与项目启动成功的模块的数据源配置进行对比,检查出不同,改为一样即可......
  • Android面试「25K—30K」的坑位,面试官喜欢问些什么?
    前言掉帧监控,函数插桩,慢函数检测,ANR监控,启动监控……这些让Android开发者们头皮发麻的内容,如今可都成为了大厂中面试必问题目:用什么机制去监控,在哪里函数插桩,反射调用用哪个类哪个方法和哪个属性?这些问题恐怕是会难倒一大批向高阶进军的开发者。目前大公司的app开发都要基于模块化......