-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path수 찾기.cpp
79 lines (66 loc) · 1.2 KB
/
수 찾기.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
int n, m, target;
int a[100000];
int answer[100000];
void QuickSort(int front, int end)
{
if (front >= end)
return;
int key = front;
int left = front + 1;
int right = end;
int temp;
while (left <= right)
{
while (left <= end && a[left] <= a[key])
++left;
while (right > front && a[right] >= a[key])
--right;
if (left > right)
{
temp = a[right];
a[right] = a[key];
a[key] = temp;
}
else
{
temp = a[left];
a[left] = a[right];
a[right] = temp;
}
}
QuickSort(front, right - 1);
QuickSort(right + 1, end);
}
int Binary_Search(int front, int end, int target)
{
if (front > end)
return -1;
int mid = (front + end) / 2;
if (a[mid] == target)
return mid;
else if (a[mid] > target)
return Binary_Search(front, mid - 1, target);
else
return Binary_Search(mid + 1, end, target);
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
QuickSort(0, n - 1);
scanf("%d", &m);
for (int i = 0; i < m; ++i)
{
scanf("%d", &target);
if (Binary_Search(0, n - 1, target) != -1)
answer[i] = 1;
else
answer[i] = 0;
}
for (int i = 0; i < m; ++i)
printf("%d \n", answer[i]);
return 0;
}