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;
}