题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为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的更多相关文章

  1. 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 ...

  2. PAT 1078 Hashing[一般][二次探查法]

    1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...

  3. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  4. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  5. 1078 Hashing (25 分)

    1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...

  6. PAT甲题题解-1078. Hashing (25)-hash散列

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

  7. PAT 1145 1078| hashing哈希表 平方探测法

    pat 1145: 参考链接 Quadratic probing (with positive increments only) is used to solve the collisions.:平方 ...

  8. 1078. Hashing (25)

    时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...

  9. PAT 1078. Hashing

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

随机推荐

  1. Treflection04_面试题

    1. package reflectionZ; import java.lang.reflect.Field; import java.lang.reflect.Method; public clas ...

  2. spring: spittr实例 构建简单的web应用

    我的环境是: jdk8, spirng4 之前照者书上说的做了,不得成功,于是网上百度,不得其然. 后来看到一篇文章,甚是所感.https://segmentfault.com/q/101000000 ...

  3. WPF的外观装饰类——Border

    public class Border : System.Windows.Controls.Decorator 说明:在另一个元素的周围绘制边框.背景或同时绘制二者.

  4. Node.js小白开路(一)-- Buffer篇

    Buffer是nodeJS中的二进制缓存操作模块内容.先来看一段简短的代码. // 创建一个长度为 10.且用 0 填充的 Buffer. const buf1 = Buffer.alloc(10); ...

  5. hdu4619

    题解: 最大独立集问题 显然对于每一对交叉的建边 然后求出最大独立集 最大独立集=n-最大匹配 代码: #include<cstdio> #include<cmath> #in ...

  6. react-redux: counter

    store: import {createStore,applyMiddleware, compose} from "redux"; import thunk from " ...

  7. 【Codeforces】894D. Ralph And His Tour in Binary Country 思维+二分

    题意 给定一棵$n$个节点完全二叉树,$m$次询问,每次询问从$a$节点到其它所有节点(包括自身)的距离$L$与给定$H_a$之差$H_a-L$大于$0$的值之和 对整棵树从叶子节点到父节点从上往下预 ...

  8. 发现的好东西——bitset

    先向各位大佬介绍一个水题 任何一个正整数都可以用2的幂次方表示.例如 137=2^7+2^3+2^0 同时约定方次用括号来表示,即a^b 可表示为a(b). 由此可知,137可表示为: 2(7)+2( ...

  9. MyEclipse Tern was unable to complete your request in time

    1.错误描述 2.错误原因 由错误提示可知,是由于MyEclipse Tern不能及时完成回复 3.解决办法 (1)Window--->Preferences--->MyEclipse-- ...

  10. C++中strftime()的详细说明

    我们可以使用strftime()函数将时间格式化为我们想要的格式.它的原型如下: size_t strftime( char *strDest, size_t maxsize, const char  ...