zoukankan      html  css  js  c++  java
  • 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 ( 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 (≤) and N (≤) 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 #define _CRT_SECURE_NO_WARNINGS  
      2 #include<stdio.h>
      3 #include<malloc.h>
      4 #include<math.h>
      5 #define MAXTABLESIZE 20000
      6 
      7 typedef int ElementType;
      8 typedef enum { Legitimate, Empty, Deleted } EntryType;
      9 typedef struct HashEntry Cell;
     10 struct HashEntry
     11 {
     12     ElementType Data;
     13     EntryType Info;
     14 };
     15 
     16 typedef struct HblNode* HashTable;
     17 struct  HblNode
     18 {
     19     int TableSize;
     20     Cell* Cells;
     21 };
     22 
     23 int NextPrime(int N)
     24 {
     25     if (N == 1)
     26         return 2;
     27     int p = (N % 2) ? N + 2 : N + 1;
     28     int i;
     29     while (p<=MAXTABLESIZE)
     30     {
     31         for (i = (int)sqrt(p); i > 2; i--)
     32             if (p % i == 0)
     33                 break;
     34         if (i == 2)break;
     35         else
     36             p += 2;
     37     }
     38     return p;
     39 }
     40 int Hash(int Key, int TableSize)
     41 {
     42     return Key % TableSize;
     43 }
     44 HashTable CreateHashTable(int TableSize)
     45 {
     46     HashTable H;
     47     H = (HashTable)malloc(sizeof(struct HblNode));
     48     H->TableSize = NextPrime(TableSize);
     49     H->Cells = (Cell*)malloc(H->TableSize * sizeof(Cell));
     50     for (int i = 0; i < H->TableSize; i++)
     51         H->Cells[i].Info = Empty;
     52     return H;
     53 }
     54 
     55 int Find(HashTable H, ElementType Key)
     56 {
     57     int NewPos, CurPos;
     58     int CNum = 0;
     59     NewPos = CurPos = Hash(Key, H->TableSize);
     60     while (H->Cells[NewPos].Info!=Empty&&H->Cells[NewPos].Data!=Key)
     61     {
     62         ++CNum;
     63         int Flag = 0;
     64         NewPos = CurPos+CNum * CNum;
     65         if (CNum>=H->TableSize)
     66             return -1;
     67         while (NewPos >= H->TableSize)
     68             NewPos -= H->TableSize;
     69     }
     70     return NewPos;
     71 }
     72 
     73 int Insert(HashTable H, ElementType Key)
     74 {
     75     int Pos = Find(H, Key);
     76     if (Pos ==-1)
     77         return -1;
     78     if (H->Cells[Pos].Info != Legitimate)
     79     {
     80         H->Cells[Pos].Data = Key;
     81         H->Cells[Pos].Info = Legitimate;
     82     }
     83     return Pos;
     84 }
     85 
     86 int main()
     87 {
     88     int M, N;
     89     scanf("%d %d", &M, &N);
     90     HashTable H = CreateHashTable(M);
     91     int i;
     92     for (i = 0; i < N-1; i++)
     93     {
     94         int num;
     95         scanf("%d", &num);
     96         int Pos = Insert(H,num);
     97         if (Pos != -1)
     98             printf("%d ", Pos);
     99         else
    100             printf("- ");
    101     }
    102     int num;
    103     scanf("%d", &num);
    104     int Pos = Insert(H, num);
    105     if (Pos != -1)
    106         printf("%d", Pos);
    107     else
    108         printf("-");
    109     return 0;
    110 }
    View Code
  • 相关阅读:
    用户模版
    VC让对话框显示就最大化
    C#中char[]与string之间的转换
    c#调用C++DLL参数对应
    解决:对 PInvoke 函数的调用导致堆栈不对称问题 <转载>
    git 命令的使用
    me 云面试
    Django 自定义模板语法
    Django session 源码流程
    云计算的介绍
  • 原文地址:https://www.cnblogs.com/57one/p/12071432.html
Copyright © 2011-2022 走看看