[백준] 1541, 1920 - Python3

shsh·2021년 10월 2일

백준

목록 보기
8/45

1920. 수 찾기

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

내 풀이 - 성공

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

A.sort()

for b in B:
    TF = 0
    l = 0
    r = N-1
    while l <= r:
        m = (l+r) // 2
        if A[m] == b:
            TF = 1
            break
        if A[m] < b:
            l = m+1
        else:
            r = m-1
    if TF:
        print(1)
    else:
        print(0)

A 는 정렬해주고 B 의 숫자들마다 이분탐색 돌리기

TF 로 존재하는지 판단해서 존재하면 1 출력, 아니면 0 출력


1541. 잃어버린 괄호

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

내 풀이 - 성공

inp = input().split("-")
ans = 0

first = inp.pop(0).split("+")
for n in first:
    ans += int(n)

for i in inp:
    tmp = 0
    nums = i.split("+")
    for n in nums:
        tmp += int(n)
    ans -= tmp

print(ans)

최솟값이 되려면 음수의 역할이 중요하니까 - 를 기준으로 split

맨 처음 값은 부호가 없으므로 ans 에 더해준다
=> + 들의 모임일 수도 있으니 + 를 기준으로 split 해서 ans 에 합함

나머지는 + 를 기준으로 split 한 후 합쳐서 빼주면 끝~

profile
Hello, World!

0개의 댓글