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
  • 相关阅读:
    Struts2之Action基础与配置
    关于Struts2的类型转换详解
    Struts2自定义类型转换器
    MyEclipse 快捷键
    Struts2中的ActionContext
    struts2中的action访问web对象
    5.9每日一题题解
    5.8 每日一题题解
    5.7 每日一题题解
    5.6 每日一题题解
  • 原文地址:https://www.cnblogs.com/57one/p/12071432.html
Copyright © 2011-2022 走看看