-
Notifications
You must be signed in to change notification settings - Fork 103
/
3.5.cpp
53 lines (47 loc) · 1.33 KB
/
3.5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* 题目名称:查找
* 题目来源:北京邮电大学复试上机题
* 题目链接:http://t.cn/E9g8aaR
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100 + 10;
int arr[MAXN];
bool BinarySearch(int n, int target) {
int left = 0;
int right = n - 1;
while (left <= right) {
int middle = left + (right - left) / 2; //中间位置
if (arr[middle] < target) { //小于关键字,舍弃左边
left = middle + 1;
} else if (target < arr[middle]) { //大于关键字,舍弃右边
right = middle - 1;
} else {
return true; //查找成功
}
}
return false;
}
int main() {
int n, m;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
sort(arr, arr + n); //先排序再查找
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
int target;
scanf("%d", &target);
if (BinarySearch(n, target)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
return 0;
}