1920(수찾기)-자료구조(python)

지환·2023년 10월 4일
0

백준(python)

목록 보기
52/67
post-thumbnail

출처 | https://www.acmicpc.net/problem/1920

코드

N = int(input())
A = list(map(int, input().split()))
M = int(input())
arr=list(map(int,input().split()))
A.sort()

for num in arr:
  lt, rt = 0, N-1
  isExist = False

  while lt <= rt:
    mid = (lt + rt) // 2

    if A[mid] == num:
      isExist = True
      print(1)
      break
    elif A[mid] > num :
      rt = mid - 1
    else:
      lt = mid + 1
  if not isExist:
    print(0)

(2)


N = int(input())
A = set(map(int, input().split()))
M = int(input())
arr = list(map(int, input().split()))

for num in arr:
  print(1) if num in A else print(0)

코드분석

  • 이진탐색에 대한 공부가 필요하다. 해당 부분을 참고하여 문제 조건을 풀면 된다.
profile
아는만큼보인다.

0개의 댓글