zoukankan      html  css  js  c++  java
  • pat09-散列1. Hashing (25)

    09-散列1. Hashing (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    8000 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 be "H(key) = key % TSize" where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

    Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.

    Sample Input:
    4 4
    10 6 4 15
    
    Sample Output:
    0 1 4 -
    

    提交代码

    注意边界测试!

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<queue>
     6 #include<vector>
     7 using namespace std;
     8 bool h[10005];
     9 int GetNextPrime(int num){
    10     int i,j;
    11     if(num==1){//注意边界
    12         return 2;
    13     }
    14     if(num==2){//注意边界
    15         return 2;
    16     }
    17     for(i=num;;i++){
    18         if(i%2==0){
    19             continue;
    20         }
    21         for(j=3;j*j<=i;j+=2){
    22             if(i%j==0){
    23                 break;
    24             }
    25         }
    26         if(j*j>i){
    27             return i;
    28         }
    29     }
    30 }
    31 int main(){
    32     //freopen("D:\INPUT.txt","r",stdin);
    33     int msize,n;
    34     scanf("%d %d",&msize,&n);
    35     int i,num,j;
    36     memset(h,false,sizeof(h));
    37     msize=GetNextPrime(msize);
    38     queue<int> q;
    39     for(i=0;i<n;i++){
    40         scanf("%d",&num);
    41         num=num%msize;
    42         if(!h[num]){
    43             h[num]=true;
    44             q.push(num);
    45         }
    46         else{
    47             int next;
    48             for(j=1;j<=msize/2;j++){
    49                 next=(num+j*j)%msize;
    50                 if(!h[next]){
    51                     h[next]=true;
    52                     q.push(next);
    53                     break;
    54                 }
    55             }
    56             if(j>msize/2){
    57                 q.push(-1);
    58             }
    59         }
    60     }
    61     int now;
    62     if(!q.empty()){
    63         now=q.front();
    64         q.pop();
    65         if(now==-1){
    66             printf("-");
    67         }
    68         else{
    69             printf("%d",now);
    70         }
    71     }
    72     while(!q.empty()){
    73         now=q.front();
    74         q.pop();
    75         if(now==-1){
    76             printf(" -");
    77         }
    78         else{
    79             printf(" %d",now);
    80         }
    81     }
    82     printf("
    ");
    83     return 0;
    84 }
  • 相关阅读:
    SQL Server SQLHelper帮助类
    Winform 常用的方法
    SQL Server 插入含有中文字符串出现乱码现象的解决办法
    ComboBox 中 DisplayMember 和 ValueMember 都是具体干什么的?
    HTML常用标签属性使用
    虚拟机安装windows server 2012 R2
    VS2017生成带图标的QT项目方法
    QSS 记录
    QT qss资源文件与代码分离
    pgsql 服务遇见的问题记录
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4729919.html
Copyright © 2011-2022 走看看