알고리즘/백준

백준 1920: 수 찾기 (Python)

sssbin 2022. 2. 10. 22:22

 

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

 

1920번: 수 찾기

첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들

www.acmicpc.net

 

반복문으로 이진탐색을 풀어주었다

 

import sys

n = int(sys.stdin.readline().rstrip())
a = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline().rstrip())
b = list(map(int, sys.stdin.readline().split()))

a.sort()

def binary_search(start, end, target):
    while start <= end:
        mid = (start + end) // 2

        if a[mid] == target:
            return 1
        elif a[mid] < target:
            start = mid + 1
        else:
            end = mid - 1

    return 0

for i in b:
    print(binary_search(0, n-1, i))