-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAP2binary.cpp
More file actions
50 lines (39 loc) · 787 Bytes
/
AP2binary.cpp
File metadata and controls
50 lines (39 loc) · 787 Bytes
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
#include <stdio.h>
int BinarySearch(int *A, int v, int n);
int main() {
int n, m, p;
scanf("%d", &n);
int A[n];
for(int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
int v = 0, s = 0;
scanf("%d", &v);
s = BinarySearch(A, v, n);
if(s != -1) {
printf("%d\n", s);
}
else {
printf("x\n");
}
}
return 0;
}
int BinarySearch(int *A, int v, int n) {
int l = 0, r = n - 1;
while(l <= r) {
int m = (l + r) / 2;
if(v == A[m]) {
return m;
}
else if(v < A[m]) {
r = m - 1;
}
else {
l = m + 1;
}
}
return -1;
}