728x90
import sys
sys.stdin = open('1920.txt','r')
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
M = int(input())
array = list(map(int, input().split()))

A.sort()
for num in array:

    lft, rt = 0, N-1
    isExist = False
    while lft <= rt:
        mid = ( lft+ rt ) //2
        if num == A[mid]:
            print(1)
            isExist =True
            break
        elif num < A[mid]:
            rt = mid -1 
        elif num >A[mid]:
            lft = mid +1
        
    if not isExist:
        print(0)

변수를 정하고 하는 것부터 적응이 안되었음

 

입력값 변수로 만들어 놓고

출력값 똑같이 나오도록 짜야하는데

 

문제 보고 이진탐색으로 하면 되겠다!

는 알았는데 

" 이 수들이 A안에 존재하는지 알아내면 된다. "

이부분이 이해가 안갔었다.

 

 

5
4 1 5 2 3
5
1 3 7 9 5

A리스트를 정렬해서 순서대로 해놓고

거기서 array의 값들을 하나하나 찾아보는 방식!

A안에 존재하면 1

없으면 0

 

이진탐색 방법으로 할것이고 

array전체를 돌기때문에 for문

 

 

https://velog.io/@deannn/BOJ-%EB%B0%B1%EC%A4%80-1920%EB%B2%88-%EC%88%98-%EC%B0%BE%EA%B8%B0-Python

 

----

 

참고

c = '1,2'

# print(list(map(int, c.split(',')))) #split은 문자열에만 된다. 배열을 하고 싶으면 join

#결과

[1,2] 

 

 

728x90

+ Recent posts