본문 바로가기
프로그래밍/코딩테스트 알고리즘

[greedy] 1이 될때까지

by Leeys 2022. 3. 19.
반응형

이것이 취업을 위한 코딩테스트다 with 파이썬

 

 

1. 내 정답

n, k = list(map(int, input().split()))

result = 0

while n>= k:
    while n % k != 0:
        n-=1
        result += 1
    
    n //= k
    result += 1
    

while n>1:
    n-=1
    result += 1

print(result)

 

2. 원래 답

n, k = map(int, input().split())
result = 0

while True:
    
    target = (n // k) * k
    result += (n - target)
    n = target
    
    if n < k:
        break
    result += 1
    n //= k

result += (n-1)
print(result)
반응형

'프로그래밍 > 코딩테스트 알고리즘' 카테고리의 다른 글

[greedy] 숫자 카드 게임  (0) 2022.03.19
[greedy] 큰 수의 법칙  (0) 2022.03.19

댓글