二次方探测解决冲突
一开始理解错了,难怪一直WA。
先寻找key%TSize的index处,如果冲突,那么依此寻找
(key+j*j)%TSize的位置,j=1~TSize-1
如果都没有空位,则输出'-'

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int maxn=; //之前设置的是10005,然后比10000大的第一个素数是10007
int isprime[maxn];
int prime[maxn];
int cnt=;
int hashing[maxn]; //hash表
/*
素数筛选法,筛选出素数,在存储
然后用二分查找,用来查找比MSize大的最小的素数
*/
void init(){
for(int i=;i<maxn;i++)
isprime[i]=;
isprime[]=;
for(int i=;i<maxn;i++){
if(isprime[i]){
for(int j=i*;j<maxn;j+=i)
isprime[j]=;
}
}
for(int i=;i<maxn;i++){
if(isprime[i]){
prime[cnt++]=i;
//printf("%d\n",i);
}
}
} /*
从素数中找出比给定的MSize大的最小素数
*/
int binarySearch(int val){
int l=,r=cnt-,mid;
while(l<r-){
mid=(l+r)>>;
if(val<=prime[mid])
r=mid;
else
l=mid;
}
return prime[r];
}
int main()
{
int m,n,TSize;
init();
scanf("%d %d",&m,&n);
if(m==)
m=; //如果m=1,则二分查找出来的是3,所以先要处理下
if(!isprime[m]){
TSize=binarySearch(m);
}
else
TSize=m; //printf("%d\n",TSize);
int a[maxn];
memset(hashing,,sizeof(hashing));
int pos,key;
for(int i=;i<n;i++){
scanf("%d",&key);
pos=-;
int idx;
for(int j=;j<TSize;j++){
idx=(key+j*j)%TSize;
if(!hashing[idx]){
hashing[idx]=;
pos=idx;
break;
}
}
if(i==){
printf("%d",pos);
}
else{
if(pos==-)
printf(" -");
else
printf(" %d",pos);
}
}
return ;
}

PAT甲题题解-1078. Hashing (25)-hash散列的更多相关文章

  1. PAT甲题题解-1010. Radix (25)-二分搜索

    题意:给出n1和n2,以及其中一个数的进制,问另一个数是多少进制的情况下,才会是两个数相等.不存在的话,则输出Impossible 这题思路很简单,但是要考虑的比较多,在简单题里面算是比较好的. 有两 ...

  2. PAT甲题题解-1032. Sharing (25)-链表水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  3. PAT甲题题解-1003. Emergency (25)-最短路径+路径数目

    给出n个城市,m条边,起始点c1和目的点c2接下来给出n个城市的队伍数以及m条双向边问你求c1到c2的所有最短路径数目,以及其中经过的最多队伍数 先最短路dijkstra,同时建立vector数组pr ...

  4. PAT甲题题解-1029. Median (25)-求两序列的中位数,题目更新了之后不水了

    这个是原先AC的代码,但是目前最后一个样例会超内存,也就是开不了两个数组来保存两个序列了,意味着我们只能开一个数组来存,这就需要利用到两个数组都有序的性质了. #include <iostrea ...

  5. PAT甲题题解-1070. Mooncake (25)-排序,大水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  6. PAT Basic 1043 输出PATest (20分)[Hash散列]

    题目 给定⼀个⻓度不超过10000的.仅由英⽂字⺟构成的字符串.请将字符重新调整顺序,按"PATestPATest-."这样的顺序输出,并忽略其它字符.当然,六种字符的个数不⼀定是 ...

  7. PAT甲题题解-1006. Sign In and Sign Out (25)-找最小最大

    判断哪个人最早到,哪个人最晚走水,就是找最大值最小值 #include <iostream> #include <cstdio> #include <algorithm& ...

  8. PAT甲题题解-1012. The Best Rank (25)-排序水题

    排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...

  9. PAT甲题题解-1033. To Fill or Not to Fill (25)-模拟

    模拟先说一下例子,最后为方便起见,在目的地安增加一个费用为0的加油站0 1 2 3 4 5 6 7 87.1 7.0 7.2 6.85 7.5 7.0 7.3 6.0 00 150 200 300 4 ...

随机推荐

  1. Linux磁盘空间占满问题快速定位

    1.df -h命令查看系统盘与各个磁盘的占用空间比率 [tidb@:vg_adn_tidbCkhsTest:172.31.30.62 /dev]$df -Th Filesystem Type Size ...

  2. Python pandas & numpy 笔记

    记性不好,多记录些常用的东西,真·持续更新中::先列出一些常用的网址: 参考了的 莫烦python pandas DOC numpy DOC matplotlib 常用 习惯上我们如此导入: impo ...

  3. ArcGIS pro2.3中添加天地图底图

    应用背景: 很多时候,我们需要使用网络上的遥感影像或者百度地图.天地图等在线地图做一些矢量化工作或者其他. 笔者见过很多人都是把百度地图截图,然后把图片导如Arcmap或者Arcgis pro中,然后 ...

  4. Python3编写网络爬虫02-基本请求库requests的使用

    一.requests 库使用 需要安装 pip install requests import requests #导入requests库 request = requests.get("h ...

  5. [提权]域内提权神器 MS14-068 完整EXP

     可以让任何域内用户提升为域管理员     c:\python27\python.exe ms14-068.py -u k8test3@k8.local -p k8team!@# -s S-1-5-2 ...

  6. 【Python求助】在eclipse和pycharm中,通过adb install安装中文名字APK时老是报错,如何解决

    # -*- coding: utf-8 -*- import os import sys import subprocess import time from uiautomator import d ...

  7. 使用<button></button>标签

    使用<button></button>标签一定要记住给它设置type,因为它默认的type=“submit”,会提交表单,设置如下 <button type=" ...

  8. Oracle 解决【ORA-01704:字符串文字太长】

    错误提示:oracle在toad中执行一段sql语句时,出现错误‘ORA-01704:字符串文字太长’.如下图: 原因:一般为包含有对CLOB字段的数据操作.如果CLOB字段的内容非常大的时候,会导致 ...

  9. 微信web开发的上传图片js接口

    $('.chooseImage').click(function(){ wx.chooseImage({ count: pic_num, // 默认9,大于9也是显示9 sizeType: ['com ...

  10. VBA删除 语法

    Option Explicit '清空数据  Private Sub CommandButton1_Click() Dim qknum As Integer  '选择是或者否 来确认删除数据 '中对话 ...