백준 2877번: 4와 7 #Python

ColorlessDia·2025년 6월 12일

algorithm/baekjoon

목록 보기
571/807
K = int(input())

i = 1
a = 1
b = 2

while not(a <= K <= b):
    i += 1

    c = 2 ** i
    a = b + 1
    b = a + c - 1

number_list = [0] * i

j = 0

while 1:
    d = b - a
    
    if d == 1:

        if K % 2 == 1:
            number_list[j] = '4'
        else:
            number_list[j] = '7'

        break
    
    e = d // 2

    if a <= K <= a + e:
        b = a + e
        number_list[j] = '4'
    else:
        a = b - e
        number_list[j] = '7'

    j += 1

print(''.join(number_list))

0개의 댓글