hwooo
프로그래머스 (C/C++) 148653 : 마법의 엘리베이터 본문
https://school.programmers.co.kr/learn/courses/30/lessons/148653#


프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr


풀이
처음엔 DP라고 생각했는데 5를 기준으로 반올림하는 규칙이 보였다.
하지만 5일 경우 바로 윗자리가 5 이상일 때 -> 올림하는 게 다음 자리 계산에 유리
5 미만일 때 -> 내림하는 게 다음 자리 계산에 유리
의 규칙이 있어 해당 부분만 따로 적용했다.
코드
void up(int& storey, int& answer, int& locVal) {
storey += locVal;
answer += 10 - locVal;
}
void down(int& storey, int& answer, int& locVal) {
storey -= locVal;
answer += locVal;
}
int solution(int storey) {
int answer = 0;
while (storey) {
int locVal = storey % 10;
if (locVal == 5) {
if (storey % 100 < 50)
down(storey, answer, locVal);
else
up(storey, answer, locVal);
}
else if (locVal < 5)
down(storey, answer, locVal);
else
up(storey, answer, locVal);
storey /= 10;
}
return answer;
}
'Study > Algorithm' 카테고리의 다른 글
| LeetCode (C/C++) 55. Jump Game (0) | 2024.10.30 |
|---|---|
| LeetCode (C/C++) 78. Subsets (0) | 2024.10.30 |
| LeetCode (C/C++) 542. 01 Matrix (0) | 2024.10.28 |
| LeetCode (C/C++) 611. Valid Triangle Number (0) | 2024.10.24 |
| 프로그래머스 (C/C++) 132265 : 롤케이크 자르기 (0) | 2024.10.22 |