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< ...
随机推荐
- springmvc与swagger2
首先呢我们导入相关的jar包文件 为了方便copy我copy一份 <!-- 导入java ee jar 包 --> <dependency> ...
- 遍历Map的四种方式(Java)
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- Django 图片上传到数据库 并调用显示
环境:Django2.0 Python3.6.4 建立项目,数据库设置,就不说了. 直接上代码: 在models.py中,需要建立模型,这里使用了ImageField字段,用来存储图片路径,这个字段继 ...
- 在k3d上快速安装Istio,助你在本地灵活使用K8S!
作者丨Mitsuyuki Shiiba 原文链接: https://dev.to/bufferings/tried-k8s-istio-in-my-local-machine-with-k3d-52g ...
- sklearn概述
Simple and efficient tools for predictive data analysis Accessible to everybody, and reusable in var ...
- Javaweb编程
首先是题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要 ...
- oracle中plsql练习-----在控制台输出1到100以内的素数。
一.思路:首先需要知道素数的概念即质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 中心思想是,外循环所有的自然数,内循环折半查询,增加代码的速度,注意:从1开始,需要大于1,但是pl ...
- 最全ElasticSearch6.5白金版本从构建ELK、集群搭建到和Mybatis结合操作详细讲解
1.安装ElasticSearch6.5.1 解压相关的ElasticSearch6.5.1的tar包到目录下,如果我们需要使用JDBC来连接的话是需要到白金版以上的,以下为将基础版破解为白金版的方法 ...
- mysql8 修改root密码
Navicat工具里选中mysql数据库 执行: ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassward'; //newpassward 新密 ...
- Who Gets the Most Candies? POJ - 2886(线段树单点更新+区间查询+反素数)
预备知识:反素数解析 思路:有了反素数的解法之后就是线段树的事了. 我们可以用线段树来维护哪些人被淘汰,哪些人没被淘汰,被淘汰的人的位置,没被淘汰的人的位置. 我们可以把所有人表示为一个[1,n]的区 ...