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<stdio.h>
#include<math.h> int main()
{
int i,MSize,n,j;
bool isprime[];
bool isexit[];
isprime[] = false;
for(i= ;i<=;i++)
{
bool is = true;
for(j=;j<=sqrt(i*1.0);j++)
{
if(i % j == )
{
is = false;
break;
}
} isprime[i] = is;
//if(is) printf("%d ",i);
}
while(scanf("%d%d",&MSize,&n)!=EOF)
{
while(isprime[MSize]==false)
++MSize; for(i = ;i<MSize ;i++)
isexit[i] = false; bool fir =true;
for(i = ;i<n;i++)
{
int tem;
scanf("%d",&tem);
if(isexit[tem%MSize]==false)
{
if(fir)
{
printf("%d",tem%MSize);
fir = false;
}
else
{
printf(" %d",tem%MSize);
}
isexit[tem%MSize] = true;
}
else
{
bool find = false;
for(j = ;j <MSize;j++)
{
if(isexit[(tem+j*j)%MSize]==false)
{
find = true;
printf(" %d",(tem+j*j)%MSize);
isexit[(tem+j*j)%MSize] = true;
break;
}
} if(!find) printf(" -");
}
} printf("\n");
}
return ;
}
1078. Hashing (25)的更多相关文章
- 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 ...
- 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)-hash散列
二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...
- PAT (Advanced Level) 1078. Hashing (25)
二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- 1078 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- pat1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
随机推荐
- css笔记15:盒子模型
1.流 流:html元素在网页中显示的顺序 标准流:在html文件中,写在前面的元素在前面显示,写在后面的html元素在后面显示. 非标准流:在html之中,当某个元素脱离了标准流,那么它处于非标准流 ...
- ng中用$http接后台接口的异步坑
最近笔者在一个项目中用ng去接后台的接口.因为前后端都是新手,前端的不懂后台,且没有经验:后端的不懂前端,也没有经验,然后接口bug百出,文档写得乱.一个接口,后台改了三次,我也是寸步难行. 首先来看 ...
- PostgreSQL解决"Abc_de_fghijkl_mn" 首字母小写去掉下划线并且下划线后面的第一个字母大写或首字母大写去掉下划线并且下划线后面的首字母大写的js
select "lower"(substr('Abc_de_fghijkl_mn', 1, 1)) || substr(replace(REGEXP_REPLACE(INITCAP ...
- oracle 设置标识列自增
设置reg_user表 userid为自增列 1.设置键 2.创建序列 3.创建触发器
- MultiMap、BidiMap及LazyMap的使用
一.MultiMap 在日常的开发工作中,我们有的时候需要构造像Map<K, List<V>>或者Map<K, Set<V>>这样比较复杂的集合类型的数 ...
- oracle 中将字符转换为blob 类型
示例如下: select id,mblx,mbmc,TO_BLOB(UTL_RAW.CAST_TO_RAW(mbsj))mbsj,qyid,qycode from tempuser.temp_cwht ...
- 区间求mex的两种方法
区间求mex的两种方法 1.莫队+分块/莫队+二分+树状数组 2.线段树 预处理1-i的sg值(用一个vis数组,一个cur指针) 预处理nxt数组(a[nxt[i]]=a[i]) 枚举左端点l, 考 ...
- 【解决】该任务映像已损坏或已篡改。(异常来自HRESULT:0x80041321)
把系统升级到Windows 10,体验了一番Windows 10.感觉不怎么好用退回到了Windows 7,发现我原来自定义的任务计划没有按时执行,于是打开任务计划,弹出了下面的对话框[该任务映像已损 ...
- 简单遗传算法求解n皇后问题
版权声明:本文为博主原创文章,转载请注明出处. 先解释下什么是8皇后问题:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法.在不 ...
- Git CMD - fetch: Download objects and refs from another repository
命令格式 git fetch [<options>] [<repository> [<refspec>…]] git fetch [<options> ...