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 ( 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 (≤) and N (≤) 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 -
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define MAXTABLESIZE 20000 typedef int ElementType;
typedef enum { Legitimate, Empty, Deleted } EntryType;
typedef struct HashEntry Cell;
struct HashEntry
{
ElementType Data;
EntryType Info;
}; typedef struct HblNode* HashTable;
struct HblNode
{
int TableSize;
Cell* Cells;
}; int NextPrime(int N)
{
if (N == )
return ;
int p = (N % ) ? N + : N + ;
int i;
while (p<=MAXTABLESIZE)
{
for (i = (int)sqrt(p); i > ; i--)
if (p % i == )
break;
if (i == )break;
else
p += ;
}
return p;
}
int Hash(int Key, int TableSize)
{
return Key % TableSize;
}
HashTable CreateHashTable(int TableSize)
{
HashTable H;
H = (HashTable)malloc(sizeof(struct HblNode));
H->TableSize = NextPrime(TableSize);
H->Cells = (Cell*)malloc(H->TableSize * sizeof(Cell));
for (int i = ; i < H->TableSize; i++)
H->Cells[i].Info = Empty;
return H;
} int Find(HashTable H, ElementType Key)
{
int NewPos, CurPos;
int CNum = ;
NewPos = CurPos = Hash(Key, H->TableSize);
while (H->Cells[NewPos].Info!=Empty&&H->Cells[NewPos].Data!=Key)
{
++CNum;
int Flag = ;
NewPos = CurPos+CNum * CNum;
if (CNum>=H->TableSize)
return -;
while (NewPos >= H->TableSize)
NewPos -= H->TableSize;
}
return NewPos;
} int Insert(HashTable H, ElementType Key)
{
int Pos = Find(H, Key);
if (Pos ==-)
return -;
if (H->Cells[Pos].Info != Legitimate)
{
H->Cells[Pos].Data = Key;
H->Cells[Pos].Info = Legitimate;
}
return Pos;
} int main()
{
int M, N;
scanf("%d %d", &M, &N);
HashTable H = CreateHashTable(M);
int i;
for (i = ; i < N-; i++)
{
int num;
scanf("%d", &num);
int Pos = Insert(H,num);
if (Pos != -)
printf("%d ", Pos);
else
printf("- ");
}
int num;
scanf("%d", &num);
int Pos = Insert(H, num);
if (Pos != -)
printf("%d", Pos);
else
printf("-");
return ;
}
1078 Hashing (25分)的更多相关文章
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PTA 11-散列2 Hashing (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing (25分) The task of this pro ...
- PAT甲题题解-1078. Hashing (25)-hash散列
二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...
- 1078. Hashing (25)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...
- 5-17 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
随机推荐
- C# .Net Core 3.1 中关于Process.Start 启动Url链接的问题
WPF 项目迁移到.Net Core中时居然出了一堆问题...(很无语) 今天在使用的时候居然发现Process.Start居然打不开Url链接了? 报 找不到指定文件 的异常?! 一.bug重现 首 ...
- 五分钟学Java:如何才能学好Java Web里这么多的技术
原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 系列文章介绍 本文是<五分钟学Java>系列文章的一篇 本系列文章主要围绕Java程序员必须掌握的核心技能,结合我个人三年 ...
- 学会了这些redis知识点,面试官会觉得你很nb(转自十年技术大牛)
是数据结构而非类型 很多文章都会说,redis支持5种常用的数据类型,这其实是存在很大的歧义.redis里存的都是二进制数据,其实就是字节数组(byte[]),这些字节数据是没有数据类型的,只有把它们 ...
- 一个和与后台数据连接的模板get post put 以及延伸的query
/* example: require.config({ paths: { "httpClient": "../../core/http-client" } } ...
- VS配置C++依赖包
处理好三个东西 1.头文件,Configuration Properties → VC++ Directories → Include Directories 2.静态库,Configuration ...
- Eclipse新建项目介绍
最近在用Eclipse,对于一个新手来说,新建项目时出现五花八门的名字,该选择哪个进行创建呢?今天小编抱着学习的态度,顺便整理分享给大家. 选择File->New->Project... ...
- vscode 配置c++记录
c_cpp_properties.json { "configurations": [ { "name": "MinGW", "i ...
- 免费开源数字货币交易所——基于Java开发的比特币交易所 | BTC交易所 | ETH交易所 | 数字货币交易所
本项目是基于Java开发的比特币交易所 | BTC交易所 | ETH交易所 | 数字货币交易所 | 交易平台 | 撮合交易引擎.本项目基于SpringCloud微服务开发,可用来搭建和二次开发数字货币 ...
- AQS机制
一,Lock接口 锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能够防止多个线程同时访问共享资源(但是有些锁可以允许多个线程并发的访问共享资源,比如读写锁).在Lock接口出现之前,Java ...
- eclipse-JEE配置Tomcat并发布第一个项目
一.配置过程 Window--preferences--Server--Runtime Environment, 然后点击add 我下载的是Tomcat7.0,选择你的版本就行了 选择Tomcat的安 ...