哈希查找(直接寻址法)

Problem Description

根据输入的int 数组建立哈希表,然后查找是否存在相应元素。

Input Description

第一行为测试数据的组数n, 下面有n组测试数据。对于每组测试数据,第一行为用空格隔开的int数列,数量不超过1,000,000,下面一行为查找数目m, 接下来的m行为m个需要查找的数(int型)。

Output Description

每组测试数据输出m行,如果存在相应的元素,输出“Yes.”,否则输出“No.”。

Sample Input

1
1 2 4 5 3 6
2
1
7

Sample Output

Yes.
No.
​#include<stdio.h>
#include<stdbool.h>

int hash[1000000] = {0};

void hash_Set(int data, int num[1000000])
{
   num[data] = 1;
}

bool hash_search(int data, int num[500000])
{
   if(num[data] == 1)
   {
      return true;
   }
   return false;
}

int main()
{
  int m;
  scanf("%d", &m);
  for(int i = 0; i < m; i++)
  {
    while(1)
    {
       int data;
       char tmp;
       scanf("%d", &data);
       hash_Set(data, hash);
       scanf("%c", &tmp);
       if(tmp == '\n')
       {
          break;
       }
    }
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        int test;
       scanf("%d", &test);
       bool ans;
       ans = hash_search(test, hash);
       if(ans == true)
       {
          printf("Yes.\n");
       }
       else
       {
          printf("No.\n");
       }
    }
       for (int i = 0; i < 1000000; i++)
        { // 清空哈希表
            hash[i] = 0;
        }
  }

  return 0;
}

​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y O L O.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值