[파이썬/python] 프로그래머스 : 예상 대진표 (Lv.2)

import math
def solution(n,a,b):
    cnt = 2
    ans = 1
    while ans < n:
        if math.ceil(a/cnt) == math.ceil(b/cnt) or ans == n :
            print(ans)
            break
        elif math.ceil(a/cnt) != math.ceil(b/cnt):
            ans += 1
            cnt *= 2
    return ans

처음에

aa = math.ceil(a/(n//2))
bb = math.ceil(b/(n//2))

이런 식으로 변수를 따로 만들어서 계속 풀리지가 않았다 이렇게 하면 안 된다;;

 

위 코드 깔끔하게 정리

import math
def solution(n,a,b):
    cnt = 2
    ans = 1

    while math.ceil(a/cnt) != math.ceil(b/cnt):
            ans += 1
            cnt *= 2
    return ans

 

comment