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< ...
随机推荐
- CSS 文本截断方案
单行截断 .ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 此方法兼容到ie6过.不过只能单行 ...
- 03-influxdb原理
influxdb基本操作 1. influxdb与传统数据库区别 influxdb 传统数据库 database 数据库 measurement 表 points 表里的一行数据 2. 基本原理 2. ...
- NoVNC API 文档翻译
原文地址:https://github.com/novnc/noVNC/blob/master/docs/API.md 时间:2019-05-21 noVNC API The interfac ...
- linux中的源码安装
前两天自己在笔记本上装了CentOs版本的虚拟机,接着要装Python3,是源码安装的挺费劲,个人总结了一些源码安装的经验,今天在这里给大家分享一下. 1. 首先准备环境,安装必要的编译工具gcc g ...
- Nodejs:md5入门介绍及crypto模块的应用
简介 MD5(Message-Digest Algorithm)是计算机安全领域广泛使用的散列函数(又称哈希算法.摘要算法),主要用来确保消息的完整和一致性.常见的应用场景有密码保护.下载文件校验等. ...
- flask连接数据库的URI书写格式
1. MySQL mysql://username:password@hostname/database 2. PostgreSQL postgresql://username:password@ho ...
- ubuntu下使用apt-get install安装软件的安装位置
在ubuntu下使用 apt-get install 或 apt install 下载安装软件,软件下载及安装后的目录.: A.下载的软件的存放位置:/var/cache/apt/archives B ...
- echarts legend文字配置多个颜色(转)
困扰很久的问题终于解决了 oh yea! echarts legend文字配置多个颜色legend: {data: [{name:‘直接访问’,icon : ‘circle’,textStyle: { ...
- WebRTC的RTCPeerConnection()原理探析
从getUserMedia()到RTCPeerConnection(),自认为难度陡增.我想一方面是之前在Linux平台上学习ROS调用摄像头时,对底层的外设接口调用.摄像头参数都有学习理解:另一方面 ...
- Web_XML
第1章 XML简介 “当 XML(扩展标记语言)于 1998 年 2 月被引入软件工业界时,它给整个行业带来了一场风暴.有史以来第一次,这个世界拥有了一种用来结构化文档和数据的通用且适应性强的格式,它 ...