https://www.acmicpc.net/problem/1920
import sys
n = sys.stdin.readline()
data = list(map(int, sys.stdin.readline().split(" ")))
m = sys.stdin.readline()
find_data = list(map(int, sys.stdin.readline().split(" ")))
data.sort()
# 반복문
def binary_search(target, data):
start = 0
end = len(data) - 1
while start <= end:
mid = (start + end) // 2
if data[mid] == target:
return 1
elif data[mid] < target:
start = mid + 1
else:
end = mid - 1
return 0
for t in find_data:
print(binary_search(t, data))
import sys
n = sys.stdin.readline()
data = list(map(int, sys.stdin.readline().split(" ")))
m = sys.stdin.readline()
find_data = list(map(int, sys.stdin.readline().split(" ")))
data.sort()
# 재귀
def binary_search(target, data, start, end):
if start > end:
return 0 # 존재하지 않음
mid = (start + end) // 2
if data[mid] == target:
return 1 # 존재
elif data[mid] < target:
start = mid + 1
else:
end = mid - 1
return binary_search(target, data, start, end)
start = 0
end = len(data) -1
for t in find_data:
print(binary_search(t, data, start, end))
728x90
반응형
'🔻PS > Baekjoon' 카테고리의 다른 글
[Baekjoon] 백준 1654 랜선 자르기 Python (0) | 2024.05.12 |
---|---|
[Baekjoon] 백준 2805 나무 자르기 Python (0) | 2024.05.11 |
[Baekjoon] 백준 10451 순열 사이클 Python (0) | 2024.05.10 |
[Baekjoon] 백준 11724 연결 요소의 개수 Python (0) | 2024.05.04 |
[Baekjoon] 백준 2178 미로 탐색 Python (0) | 2024.05.01 |