PAT (Advanced Level) Practice - 1078 Hashing(25 分)

简介: PAT (Advanced Level) Practice - 1078 Hashing(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:平方探测法处理哈希表,但是该题只能正增长:with positive increments only。即,hi=(h(key)+i*i)%m (0 ≤ i ≤ m-1)

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int isPrime(int x)
{
    if(x<2) return 0;
    int len=sqrt(x);
    for(int i=2;i<=len;i++)
        if(x%i==0) return 0;
    return 1;
}
int vis[20000];
int main()
{
    int p,n,a,idx,f;
    scanf("%d%d",&p,&n);
    while(!isPrime(p)) p++;
    vector<int> v;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a); f=0;
        for(int j=0;j<p;j++)
        {
            idx=abs(a+j*j)%p;
            if(!vis[idx])
            {
                v.push_back(idx);
                vis[idx]=1;
                f=1;
                break;
            }
//            idx=abs(a-j*j)%p;
//            if(!vis[idx])
//            {
//                v.push_back(idx);
//                vis[idx]=1;
//                f=1;
//                break;
//            }
        }
        if(!f) v.push_back(-1);
    }
    for(int i=0;i<v.size()-1;i++)
    {
        if(v[i]!=-1) printf("%d ",v[i]);
        else printf("- ");
    }
    if(v[v.size()-1]!=-1) printf("%d\n",v[v.size()-1]);
    else printf("-\n");
    return 0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
96 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
120 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
134 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
119 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
142 0
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
PAT (Advanced Level) Practice - 1049 Counting Ones(30 分)
120 0
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
124 0
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
112 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
115 0
PAT (Advanced Level) Practice - 1145 Hashing - Average Search Time(25 分)
PAT (Advanced Level) Practice - 1145 Hashing - Average Search Time(25 分)
120 0