백준 풀이

백준 1920 풀이(이분 탐색(binary search))

ag2개발자 2022. 2. 1. 14:01
def binary_search(arr,target,start,end):
    while start<=end:
        mid=(start+end)//2
        if arr[mid]==target:
            return 1
        elif arr[mid]>target:
            end = mid-1
        else:
            start=mid+1
    return 0

n=int(input())
a = list(map(int, input().split()))
a.sort()
m=int(input())
b=list(map(int,input().split()))

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

이분탐색을 할 줄 아냐는 문제...

나동빈 님의 "이것이 코딩 테스트다"를 참조하여 코딩했습니다.

생각하는 프로그래밍의 저자 존 벤틀리에 의하면 이진탐색 코드를 제대로 작성한 프로그래머는

10%내외라고 할정도로 딱 보면 쉬워 보이지만 막상 작성해보면 실수가 꼬옥 나오니 외워두자!

'백준 풀이' 카테고리의 다른 글

백준 10250번 python  (0) 2022.08.18
백준 10926 파이썬  (0) 2022.08.18
백준 2292번 파이썬  (0) 2022.08.18
백준 2292 풀이  (0) 2022.02.02
백준 2839 풀이  (0) 2022.02.01