OJ3-2 B. Hash Table

B. Hash Table
运行时间限制: 1000 运行内存限制: 65536
作者: scsxuke 是否specialjudge: False
题目描述
Create a hash table (Open Addressing) from the given sequence and output the Londing Density and Average Search Time (AST). The hash function is H(key)= key % 13 and use linear probing to sovle the collision.
Input a sequence of integer seperated by comma.
Output 2 lines, the first is Loading Density, the second is AST. (Two decimal places are reserved for the result)

输入样例
12,13,14,26,
输出样例
0.31
1.50

#include <stdio.h>
#include <stdlib.h>
#define full 1
#define empty 0
#define delete 2
struct Node;
typedef struct Node *ptrtoNode;

struct Node
{
    int x;
    int kind;
};
int insert(int x, ptrtoNode array[]);
int main()
{
    char ch[200];
    gets(ch);
    int nums, num; //nums 为一个数,num为一个数的某一位
    nums = num = 0;
    double times = 0;    //times 是查找的次数
    ptrtoNode array[13]; // 相当于hashtable
    for (int i = 0; i < 13; i++)
    {
        array[i] = malloc(sizeof(struct Node));
        array[i]->kind = empty;
    }
    int numbers = 0; //一共有几个数?
    for (int i = 0; ch[i] != '\0'; i++)
    {
        if (ch[i] != ',')
        {
            num = ch[i] - '0';
            nums = nums * 10 + num;
        }
        else
        {
            times = insert(nums, array) + times;
            nums = 0;
            numbers++;
        }
    }
    double LD = numbers / 13.00;
    double AST = times / numbers;
    printf("%.2lf\n", LD);
    printf("%.2lf\n", AST);
    return 0;
}
int insert(int x, ptrtoNode array[])
{
    int time = 1;
    int pos;
    pos = x % 13;
    while (array[pos]->kind != empty)
    {
        pos++;
        pos = pos % 13;
        time++;
    }
    array[pos]->x = x;
    array[pos]->kind = full;
    return time;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值