PGS / 마법의 엘리베이터 / Python
Updated:
풀면서
✅ 테스트케이스는 다 통과했다. 그런데 제출을 했는데 0점이다. 제대로 짠 것 같은데 뭘까.
import math
def solution(storey):
answer = 0
if storey % 10 == 0:
return storey // 10
storey_ = math.floor((storey/10)+0.5)*10
plus = abs(storey_ - storey)
while storey_ > 0:
answer += storey_ % 10
storey_ //= 10
return answer+plus
✅ 일의 자리만 검사하면 되는 게 아니라 모든 자릿수 마다 다 검사를 해야한다.
6~9, 0~4, 5 세 가지 케이스로 나눠서 따져봐야 한다.
code ⌨️
구글링해서 다른 코드를 참고했다.
def solution(storey):
answer = 0
storey_ = storey
while storey_ > 0:
last = storey_ % 10
if last > 5:
answer += (10-last)
storey_ += 10
elif last < 5:
answer += last
else:
if (storey_ / 10) % 10 > 5:
storey_ += 10
answer += last
storey_ //= 10
return answer
Leave a comment