zoukankan      html  css  js  c++  java
  • pat1078. Hashing (25)

    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 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<stack>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 #include<cmath>
     9 using namespace std;
    10 void getprime(int &msize){
    11     int i=msize,j;
    12     if(msize==0||msize==1||msize==2){
    13         msize=2;
    14         return;
    15     }
    16     for(;;i++){
    17         if(i%2==0){
    18             continue;
    19         }
    20         int maxl=sqrt(msize*1.0);
    21         for(j=3;j<=maxl;j+=2){
    22             if(i%j==0){
    23                 break;
    24             }
    25         }
    26         if(j>maxl){
    27             msize=i;
    28             return;
    29         }
    30     }
    31 }
    32 map<int,int> ha;
    33 bool g[10005];
    34 int main(){
    35     //freopen("D:\INPUT.txt","r",stdin);
    36     int i,msize,n,num;
    37     scanf("%d %d",&msize,&n);
    38     getprime(msize);
    39 
    40     //cout<<msize<<endl;
    41 
    42     for(i=0;i<n;i++){
    43         scanf("%d",&num);
    44         ha[i]=num%msize;
    45     }
    46     int half=msize/2;
    47     map<int,int>::iterator it=ha.begin();
    48     g[it->second]=true;
    49     printf("%d",it->second);
    50     it++;
    51     for(;it!=ha.end();it++){
    52         if(!g[it->second]){
    53             g[it->second]=true;
    54             printf(" %d",it->second);
    55         }
    56         else{
    57             for(i=1;i<=half;i++){
    58                 if(!g[(it->second+i*i)%msize]){
    59                     g[(it->second+i*i)%msize]=true;
    60                     printf(" %d",(it->second+i*i)%msize);
    61                     break;
    62                 }
    63             }
    64             if(i>half){
    65                 printf(" -");
    66             }
    67         }
    68     }
    69     printf("
    ");
    70     return 0;
    71 }
  • 相关阅读:
    C++类型转换运算符
    今天开通了博客
    计算每天工时逻辑,去掉可能重叠交叉的数据(由于前端插件存储["11:30","12:00"] 这样的json字符串)
    初次涉及左右值算法(树结构)
    MySql数据库在表中添加新字段,设置主键,设置外键,字段移动位置,以及修改数据库后如何进行部署和维护的总结
    关于使用JQuery追加Option标签时使用三元运算符添加选中属性的解决办法
    【Spring】关于SpringMvc监听的知识点
    关于使用三元运算符来判断下拉框选项是否选中的总结
    关于frameset的一些小总结
    关于上传文件的一点总结
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4783066.html
Copyright © 2011-2022 走看看