Hash-开放地址法

#include<stdio.h>
#define N 100005
int Check[N];
int main(){
	int n,num;
	scanf("%d %d",&n,&num);
	for(int i=0;i<n;i++){
		int index;
		scanf("%d",&index);
		Check[index]=1;
	}
	for(int j=0;j<num;j++){
		int key;
		scanf("%d",&key);
		if(Check[key]){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}

查找练习 hash——出现过的数字

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

有一个数据字典,里面存有n个数字(n<=100000),小明现在接到一个任务,这项任务看起来非常简单——给定m个数字,分别查询这m个数字是否出现在字典之中;但是考虑到数据量的问题,小明找到了善于编程的你,希望你可以帮他解决这个问题。
 

Input

 输入数据只有一组!
第一行包含两个整数n m,分别代表字典中数字的个数和要查询的数字的个数。
接着n行代表字典中的n个数字。
最后m表示要查询的数字。

Output

 如果某个数字存在,则输出YES,否则输出NO
 

Example Input

5 3
1
2
3
4
5

5
4
10

Example Output

YES
YES
NO
 
 

猜你喜欢

转载自blog.csdn.net/m0_37848958/article/details/78818745