[프로그래머스] 마법의 엘리베이터

단간단간·2024년 3월 27일
0

알고리즘 문제

목록 보기
22/106

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/148653
회고 : 재귀로 푼 사람 많지만, while문으로도 충분히 가능. 추가로 solution(555) = 14 나오는지 확인해보면 좋을 것.

python

def solution(storey):
    count = 0

    while True:
        storey, rest = storey // 10, storey % 10
        if rest > 5 or (rest == 5 and storey % 10 >= 5):
            count += 10 - rest
            storey += 1
        else:
            count += rest

        if storey == 0:
            break

    return count
profile
simple is best

0개의 댓글