- 题目描述:
-
输入数组长度 n
输入数组 a[1…n]
输入查找个数m
输入查找数字b[1…m]
输出 YES or NO 查找有则YES 否则NO 。
- 输入:
-
输入有多组数据。
每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m<=n<=100)。
- 输出:
-
如果在n个数组中输出YES否则输出NO。
- 样例输入:
-
5
1 5 2 4 3
3
2 5 6
- 样例输出:
-
YES
YES
NO采用了哈希的办法,玩嘛#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#define MAX 103 struct Number
{
int key;
bool isVaild;
};
Number map[MAX]; int main(int argc, char const *argv[])
{
int n, m;
while(scanf("%d",&n) != EOF) {
int temp;
for(int i = ; i < MAX; i++) {
map[i].isVaild = false;
}
for(int i = ; i < n; i++) {
scanf("%d",&temp);
int num = temp%;
while(map[num].isVaild == true) {
num++;
if(num == ) {
num = ;
}
}
map[num].key = temp;
map[num].isVaild = true;
}
scanf("%d",&m);
for(int i = ; i < m; i++) {
scanf("%d",&temp);
int num = temp%;
if(map[num].isVaild == false) {
puts("NO");
}
else {
bool flag = true;
while(flag) {
if(map[num].key == temp) {
puts("YES");
flag = false;
break;
}
num++;
if(num == ) {
num = ;
}
}
if(flag == true) {
puts("NO");
}
}
}
}
return ;
}当然,普通的办法好像速度也不慢
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#define MAX 101 int num[MAX]; int main(int argc, char const *argv[])
{
int n, m;
while(scanf("%d",&n) != EOF) { for(int i = ; i < n; i++) {
scanf("%d",&num[i]);
}
scanf("%d",&m); for(int i = ; i < m; i++) {
int temp;
scanf("%d",&temp);
bool flag = false;
for(int j = ; j < n; j++) {
if(temp == num[j]) {
puts("YES");
flag = true;
break;
}
}
if(flag == false) {
puts("NO");
}
}
}
return ;
}