Pat(Advanced Level)Practice--1078(Hashing)
来源:互联网 发布:java软件编程培训中心 编辑:程序博客网 时间:2024/06/01 08:29
Pat1078代码
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 15Sample Output:
0 1 4 -
#include<cstdio>#define MAX 10010using namespace std;int hash[MAX];int visited[MAX];int IsPrime(int n){if(n==2)return 1;if(n<2||n%2==0)return 0;for(int i=3;i*i<=n;i=i+2)if(n%i==0)return 0;return 1;}int main(int argc,char *argv[]){int i,j;int s,n;int size,index;scanf("%d%d",&s,&n);size=s;while(1){if(IsPrime(size))break;elsesize++;}for(i=0;i<n;i++){int temp;hash[i]=-1;scanf("%d",&temp);for(j=0;j<size;j++){index=(temp%size+j*j)%size;if(!visited[index]){visited[index]=1;hash[i]=index;break;}}}printf("%d",hash[0]);for(i=1;i<n;i++){if(hash[i]==-1)printf(" -");elseprintf(" %d",hash[i]);}printf("\n");return 0;}注意题目的要求是二次勘探法,还有数组不能开的小于10007,因为比10000大的最小素数是10007,以防止数组越界。
- Pat(Advanced Level)Practice--1078(Hashing)
- PAT (Advanced Level) Practise 1078 Hashing (25)
- PAT (Advanced Level) Practice 1006
- PAT (Advanced Level) Practise 1078 Hashing (25)(平方探测)
- Pat(Advanced Level)Practice--1025(PAT Ranking)
- Pat(Advanced Level)Practice--1075(PAT Judge)
- PAT Advanced Level 1078. Hashing(25)
- 【PAT】【Advanced Level】1078. Hashing (25)
- Pat(Advanced Level)Practice--1003(Emergency)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- Pat(Advanced Level)Practice--1008(Elevator)
- Pat(Advanced Level)Practice--1010(Radix)
- Pat(Advanced Level)Practice--1015(Reversible Primes)
- Pat(Advanced Level)Practice--1024(Palindromic Number)
- Pat(Advanced Level)Practice--1028(List Sorting)
- Pat(Advanced Level)Practice--1029(Median)
- Pat(Advanced Level)Practice--1030(Travel Plan)
- Pat(Advanced Level)Practice--1035(Password)
- NSDictionary: objectForKey: valueForKey:
- IO - 同步,异步,阻塞,非阻塞
- 【TJOI2013】数字根
- Day4 - chkconfig 设置系统应用程序
- c++中new和delete的使用方法
- Pat(Advanced Level)Practice--1078(Hashing)
- Face++ 第三方类库的使用文档
- Reporting Services中参数说明
- 组件热升级
- 单文档应用程序中,将基类CView转换为继承CListView
- MATLAB实验作业答案
- android数据库的创建
- C++ 常引用 const int &a 的作用和区别
- linux系统函数学习