1078 Hashing
题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移)。
思路:同1145 Hashing - Average Search Time
代码:
#include <cstdio>
#include <cmath>
#include <unordered_map>
using namespace std;
bool isPrime(int n)
{
) return false;
int sqr=(int)sqrt(n);
;i<=sqr;i++)
) return false;
return true;
}
int main()
{
int MSize,n,key;
scanf("%d%d",&MSize,&n);
while(!isPrime(MSize))
MSize++;
]={false};//exist[i]为true表示位置i已经被占据了
];
unordered_map<int,int> pos;//pos[val]存放val的位置
;i<n;i++){
scanf("%d",&key);
data[i]=key;
;
for(;d<MSize;d++){
int p=(key+d*d)%MSize;
if(exist[p]==false){
exist[p]=true;
pos[key]=p;
break;
}
}
;
}
;i<n;i++){
) printf("-");
else printf("%d",pos[data[i]]);
) printf(" ");
}
;
}
1078 Hashing的更多相关文章
- 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[一般][二次探查法]
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- 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 ...
- 1078 Hashing (25 分)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- PAT甲题题解-1078. Hashing (25)-hash散列
二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...
- PAT 1145 1078| hashing哈希表 平方探测法
pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...
- 1078. Hashing (25)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...
- PAT 1078. Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
随机推荐
- JAVA 泛型意淫之旅(二)
编译器如何处理泛型 泛型类编译后长什么样? 接上文,JAVA 泛型意淫之旅1,成功迎娶白富美后,终于迎来了最振奋人心的一刻:造娃!造男娃还是造女娃?对于我们程序猿来说,谁还在乎是男娃女娃,只要是自己的 ...
- html5新增语义化标签
注意:body.section.nav 需要h1-h6. div.header则不需要. 1):<article> 显示一个独立的文章内容. 例如一篇完整的论坛帖子,一则网站新闻,一篇博客 ...
- 条款21:必须返回对象的时候,不要妄想使其返回reference
//先看看下面这个例子 class Rational{ public: Rational(int num, int denu) :numirator(num), denumirator(denu); ...
- poj2378(dfs,树形dp)
和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...
- poj3107(dfs,树形dp)
和poj1655的方法完全一样,但是这道题的n的范围大了,用vector存图会TLE(poj没有O2编译优化),所以改用前向星来存图就可以了.. 有关树的重心,看这里:poj1655 这里解释一下前向 ...
- CentOS修改系统的默认启动模式为命令号界面
CentOS在安装桌面之后,将图形界面设置为默认模式,给PC内存带来较大压力,所以希望修改系统的默认启动模式为命令号界面,而不启动图形界面.方法如下: 1.以 root用户登录系统:或者 用su切换到 ...
- struts2.5框架使用通配符指定方法,某一个匹配不到
在学习struts框架时经常会使用到通配符调用方法,如下:但奇怪的是,在validateName请求老报404,其他的都是ok的,开始以为是配置错了,检查好久才知道: <action name= ...
- I.MX6 网卡能收不能发
/******************************************************************** * I.MX6 网卡能收不能发 * 说明: * MAC控制器 ...
- (二)canvas边框问题
lineWidth 设置边框的大小 fillStyle 设置div的颜色 strokeStyle 设置边框的颜色 注: 边框在不设置的情况下默认为1px 黑色,但是x,y轴的距离是以图形的正中心为原始 ...
- 使用python对文件中的数值进行累加
问题描述: 一个文件由若干条记录组成,记录的格式为:“num1 num2”,有时候,需要统计文件中num1对应的num2的总值.处理问题的思路 用传说中的python来处理,很方便.几行代码就可以了. ...