https://www.acmicpc.net/problem/1920
문제
내 풀이
from typing import Any, Sequence
def bin_search(a: Sequence, key: Any) -> int:
pl = 0
pr = len(a) - 1
while True:
pc = (pl + pr)//2
if a[pc] == key:
return 1
elif a[pc] < key:
pl = pc+1
else:
pr = pc - 1
if pl > pr:
break
return 0
if __name__ == "__main__":
n = int(input())
a = list(map(float, input().split()))
a.sort()
n2 = int(input())
a2 = list(map(int, input().split()))
for i in range(len(a2)):
ky = a2[i]
print(bin_search(a, ky))
출처 : Do it! 파이썬
해설
책에 잇는 이진 검색법을 그대로 적용 시켜 보았다.
정답 풀이
x
출처:
해석
'sw사관학교 정글 2기 > 02 이분탐색, 분할정복, 스택, 큐, 우선순위 큐' 카테고리의 다른 글
[이분탐색] 백준 2110번 공유기 설치 with Python3 ★★ (0) | 2021.08.13 |
---|---|
[이분탐색] 백준 2805번 나무 자르기 with Python3 ★★ (0) | 2021.08.13 |
[분할 정복] 백준 2630번 색종이 만들기 with Python3 ★★ (0) | 2021.08.13 |
[큐] 백준 11866번 요세푸스 문제0 with Python3 (0) | 2021.08.13 |
[큐] 백준 2164번 카드2 with Python3 (0) | 2021.08.13 |
댓글