Skip to content

[Algorithm] 마법의 엘리베이터 #59

Description

@hwangJi-dev

💬 문제

[코딩테스트 연습 - 마법의 엘리베이터](https://school.programmers.co.kr/learn/courses/30/lessons/148653)


💬 Idea

storey가 0이 될때까지 while문을 돌며 result를 구한다.

  1. storey를 10으로 나눈 나머지를 갖고 계산을 한다. → ex) 554: 첫 나머지는 4

  2. storey를 10으로 나눈 나머지가 6 이상일 경우, storey를 10으로 나눈 나머지가 5이면서 앞자리가 5 이상일 경우
    → 엘리베이터 상승 → 앞자리 + 1

✅ 엘리베이터가 10으로 상승하므로 result += 10 - (storey % 10)
✅ 앞자리를 올리기 위해 storey = (storey / 10) + 1
  1. storey를 10으로 나눈 나머지가 5 이하이면서 앞자리가 5보다 작을 때, storey를 10으로 나눈 나머지가 4 이하일 때
    → 엘리베이터 하강
✅ 엘리베이터가 0으로 내려가므로 result += storey % 10
✅ storey = storey / 10

💬 풀이

func solution(storey:Int)->Int{varstorey= storey
varresult=0while 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
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions