반응형

문제 : https://www.acmicpc.net/problem/1920

 

서론

이진탐색 문제

 


구현

//c++
#pragma warning (disable : 4996)

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

int a[100001];
int n, m;

int bs(int target) {
	int start = 0;
	int end = n - 1;
	while (start <= end) {
		int mid = (start + end) / 2;
		if (a[mid] == target) return 1;
		else if (a[mid] < target) start = mid + 1;
		else end = mid - 1;
	}
	return 0;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	cin >> m;
	for (int i = 0; i < m; i++) {
		int target;
		scanf("%d", &target);
		printf("%d\n", bs(target));
	}
}