博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pat1078. Hashing (25)
阅读量:4358 次
发布时间:2019-06-07

本文共 2501 字,大约阅读时间需要 8 分钟。

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 410 6 4 15
Sample Output:
0 1 4 -

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
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
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<
<
::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("\n");70 return 0;71 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4783066.html

你可能感兴趣的文章
设计模式六大原则(2):里氏替换原则
查看>>
curl应用总结 转载
查看>>
C++ 类的对象管理模型初讲
查看>>
企业应用架构读书笔记与总结
查看>>
查看系统信息命令大全
查看>>
awk,rsync,重启,maxdepth一层目录,登录,开机自启动
查看>>
代码回顾
查看>>
对一次系统上线的思考-走出“舒适区”
查看>>
【06】Cent OS 7 中部署 zabbix_server 环境
查看>>
vue 中 vue-router、transition、keep-alive 怎么结合使用?
查看>>
小常识
查看>>
TungstenSecret
查看>>
LR遇到的问题
查看>>
51nod 1010 只包含因子2 3 5的数 && poj - 1338 Ugly Numbers(打表)
查看>>
在OS X 10.11 中使用 "apue.h" (3rd Edition)
查看>>
mysql 批量插入更新
查看>>
ubuntu 实用命令收集
查看>>
[算法]分治算法(Divide and Conquer)
查看>>
mssql格式化工具——SQL PRETTY PRINTER
查看>>
datagrid删除按钮
查看>>