PAT 1078 Hashing

# include <stdio.h>
# include <algorithm>
# include <string.h>
# include <iostream>
# include <math.h>
using namespace std;
int prime[10100];
int vis[10110];
int cot;
void init_prime()
{
    memset(vis,0,sizeof(vis));
    cot=0;
    for(int i=2; i<=10100; i++)
    {
        if(!vis[i])
        {
            prime[cot++]=i;
            for(int j=i*2; j<=10100; j+=i)
                vis[j]=1;
        }
    }
}
int main()
{
    int n,size1,i,a,ans,j;
    int map[10100];
    while(~scanf("%d%d",&size1,&n))
    {
        init_prime();
        memset(map,0,sizeof(map));
        for(i=0; i<cot; i++)
        {
            if(prime[i]>=size1)
            {
                size1=prime[i];
                break;
            }
        }
        for(i=0; i<n; i++)
        {
            scanf("%d",&a);
            ans=a%size1;
            if(!map[ans])
            {
                if(i==0)
                    printf("%d",ans);
                else
                    printf(" %d",ans);
                map[ans]=1;
            }
            else/*二次探测法的公式: hi=(h(key)+i*i)%size1 1≤i≤size1-1 //即di=i2
                               即探查序列为d=h(key),d+1^2,d+2^2,…d+(size1-1)^2*/
            {
                for(j=1; j<size1; j++)
                {
                    int kk=(ans+j*j)%size1;
                    if(!map[kk])
                    {
                        map[kk]=1;
                        if(i==0)
                            printf("%d",kk);
                        else
                            printf(" %d",kk);
                        break;
                    }
                }
                if(j==size1)
                {
                    if(i==0)
                        printf("-");
                    else
                        printf(" -");

                }
            }
        }
        printf("\n");
    }
    return 0;
}

时间: 2025-01-04 06:19:46

PAT 1078 Hashing的相关文章

PAT 1078. Hashing (25)

1078. Hashing (25) The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the

1078. Hashing (25)【Hash + 探测】——PAT (Advanced Level) Practise

题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(

1078. Hashing (25)-PAT甲级真题

1078. Hashing (25)The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the

PAT 1078. 字符串压缩与解压

PAT 1078. 字符串压缩与解压 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 aba 压缩后仍然是 aba. 解压方法就是反过来,把形如 5c 这样的表示恢复为 ccccc. 本题需要你根据压缩或解压的要求,对给定字符串进行处理.这里我们简单地假设原始字符串是完全由英文字母和空格组成的非空字符串. 输入格式: 输入第一行给出一个字符,如果

PAT (Advanced Level) 1078. Hashing (25)

二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

PAT Advanced 1078 Hashing (25) [Hash ?次?探查法]

题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the maximum size of

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) { if(n<=1) return fa

1078. Hashing (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to

PAT 1078

因爲輸入反了所以WA了好多次... 真的太困了貼完睡覺. 二次探查法都忘記是什麼了,原來還有不能被插入的情況.. 等到真的要去找工作的時候再認真念一念書吧- 1 #include <vector> 2 #include <iostream> 3 #include <cmath> 4 5 using namespace std; 6 7 bool is_prime(int num){ 8 if (num == 1) 9 return false; 10 if (num =