Hashing - Average Search Time

PAT-1145

  • 需要注意本题的table的容量设置
  • 二次探测,只考虑正增量
  • 这里计算平均查找长度的方法和书本中的不同
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<set>
#include<map>
#include<cmath>
using namespace std;
const int maxn=10004;
int table[100*maxn];
int size,n,m;
bool isPrime(int n){
if(n<=1){
return false;
}
for(int i=2;i*i<=n;i++){
if(n%i==0)
return false;
}
return true;
}
int findPrime(int n){
while(!isPrime(n)){
n++;
}
return n;
}
bool insertHash(int n){
int ori=n;
bool flag=false;
for(int j=0;j<size;j++){
if(table[(n+j*j)%size]==-1){
table[(n+j*j)%size]=ori;
flag=true;
break;
}
}
return flag;
}
int findHash(int n){
//返回次数
int ans=0;
for(int j=0;j<=size;j++){
ans++;
if(table[(n+j*j)%size]==-1||table[(n+j*j)%size]==n)
break;
}
return ans;
}
int main(){
memset(table,-1,sizeof(table));
cin>>size>>n>>m;
size=findPrime(size);
for(int i=0;i<n;i++){
int a;
cin>>a;
if(!insertHash(a)){
cout<<a<<" cannot be inserted."<<endl;
}
}
int sums=0;
for(int i=0;i<m;i++){
int b;
cin>>b;
sums+=findHash(b);
}
printf("%.1lf\n",sums*1.0/m);
return 0;
}

PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突的更多相关文章

  1. PAT 1145 Hashing - Average Search Time [hash][难]

    1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...

  2. PAT 1145 Hashing - Average Search Time

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  3. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  4. [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

    1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...

  5. PAT Advanced 1145 Hashing – Average Search Time (25) [哈希映射,哈希表,平⽅探测法]

    题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...

  6. PAT 甲级 1145 Hashing - Average Search Time

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...

  7. 1145. Hashing - Average Search Time

      The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...

  8. PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  9. 1145. Hashing - Average Search Time (25)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

随机推荐

  1. poj 1696 极角排序(解题报告)

    #include<iostream> #include<cmath> #include<algorithm> using namespace std; double ...

  2. A. Little Pony and Expected Maximum

    Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she ...

  3. C++中overload 、override、overwrite 之间的区别

    Overload(重载):在C++程序中,可以将语义.功能相似的几个函数用同一个名字表示,但参数或返回值不同(包括类型.顺序不同),即函数重载.(1)相同的范围(在同一个类中):(2)函数名字相同:( ...

  4. postion:fixed和margin:0 auto的使用

    很多同学将顶部菜单固定,使用postion:fixed,然后使用margin:0 auto进行居中,发现margin:0 auto并不起作用. 通常,我们要让某元素居中,会这样做: #element{ ...

  5. js code review

    js code review https://codereview.stackexchange.com/ refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只 ...

  6. Fullscreen API All In One

    Fullscreen API All In One 全屏显示 https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API https ...

  7. React Hooks: useDebugValue All In One

    React Hooks: useDebugValue All In One useDebugValue https://reactjs.org/docs/hooks-reference.html#us ...

  8. React Hooks: useCallback All In One

    React Hooks: useCallback All In One useCallback https://reactjs.org/docs/hooks-reference.html#usecal ...

  9. 如何实现 React 模块动态导入

    如何实现 React 模块动态导入 React 模块动态导入 代码分割 webpack & code splitting https://reactjs.org/docs/code-split ...

  10. Google & Chrome console & text adventure game

    Google & Chrome console & text adventure game Google's text adventure game https://www.googl ...