-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
💬 문제
[코딩테스트 연습 - 마법의 엘리베이터](https://school.programmers.co.kr/learn/courses/30/lessons/148653)
💬 Idea
storey가 0이 될때까지 while문을 돌며 result를 구한다.
-
storey를 10으로 나눈 나머지를 갖고 계산을 한다. → ex) 554: 첫 나머지는 4
-
storey를 10으로 나눈 나머지가 6 이상일 경우, storey를 10으로 나눈 나머지가 5이면서 앞자리가 5 이상일 경우
→ 엘리베이터상승
→ 앞자리 + 1
✅ 엘리베이터가 10으로 상승하므로 result += 10 - (storey % 10)
✅ 앞자리를 올리기 위해 storey = (storey / 10) + 1
- storey를 10으로 나눈 나머지가 5 이하이면서 앞자리가 5보다 작을 때, storey를 10으로 나눈 나머지가 4 이하일 때
→ 엘리베이터하강
✅ 엘리베이터가 0으로 내려가므로 result += storey % 10
✅ storey = storey / 10
💬 풀이
func solution(storey: Int) -> Int {
var storey = storey
var result = 0
while storey != 0 {
if storey % 10 > 5 || (storey % 10 == 5 && (storey / 10) % 10 >= 5) {
result += 10 - (storey % 10)
storey = (storey / 10) + 1
} else {
result += storey % 10
storey = storey / 10
}
}
return result
}