주어진 숫자의 가장 낮은 자릿수를 확인하며,
조건을 정해 + 또는 - 를 통해 그 자릿수를 0으로 만든 후,
10으로 수를 나누어 해당 자릿수를 없애가는 식으로 문제를 풀었다.
전체 코드는 다음과 같다.
import java.util.*;
class Solution {
public int solution(int storey) {
int answer = 0;
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
int a = 10 - num;
answer += a;
storey = storey + a;
}else if(num == 5){
if(size > 1){
int next = storey % 100;
next = (next - num) / 10;
if(next >= 5){
int a = num;
answer += a;
storey = storey + a;
}else{
int a = num;
answer += a;
storey = storey - a;
}
}else{
int a = num;
answer += a;
storey = storey - a;
}
}else if(num < 5){
int a = num;
answer += a;
storey = storey - a;
}
storey = storey / 10;
}
return answer;
}
}
반복문을 통해 주어진 수가 0이 될 때까지 반복한다.
while(storey != 0){
...
}
먼저 주어진 수의 길이(자릿 수)를 구해 size
에 저장하고
주어진 수를 10으로 나눈 나머지를 구해 주어진 수의 일의 자릿수를 구한다.
256 % 10 = 6
일의 자릿수 = 6
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
...
}
구한 일의 자릿수가 특정 조건을 만족하는지 확인한다.
조건은 5보다 클 때 / 5일 때 / 5보다 작을때로 나뉜다.
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
...
}else if(num == 5){
...
}else if(num < 5){
...
}
...
}
5보다 크다면 + 를 통해 10을 만들어 해당 자릿수를 0으로 만드는 것이 최소횟수가 된다.
+ : 37 => 40 => 0 / 연산 횟수 = 3 + 4 = 7
- : 37 => 30 => 0 / 연산 횟수 = 7 + 3 = 10
몇번 + 연산을 하면 10이 되는지 구하고 그 값을 정답에 더한다.
그 후 주어진 숫자에 그 값을 더한다.
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
int a = 10 - num;
answer += a;
storey = storey + a;
}else if(num == 5){
...
}else if(num < 5){
...
}
...
}
5보다 작다면 - 를 통해 0을 만들어 해당 자릿수를 0으로 만드는 것이 최소횟수가 된다.
- : 34 => 30 => 0 / 연산 횟수 = 4 + 3 = 7
+ : 34 => 40 => 0 / 연산 횟수 = 6 + 4 = 10
몇번 - 연산을 하면 0이 되는지 구하고 그 값을 정답에 더한다.
그 후 주어진 숫자에 그 값을 뺀다.
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
...
}else if(num == 5){
...
}else if(num < 5){
int a = num;
answer += a;
storey = storey - a;
}
...
}
만약 5라면 먼저 현재 수의 크기(자릿수)가 1보다 큰지 확인한다.
1보다 작다면 남은 층수가 5층만 남았다는 뜻이므로 5를 빼는 것으로 정답에 도달한다.
그러므로 5를 정답에 더하고 5를 주어진 수에서 빼준다.
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
...
}else if(num == 5){
if(size > 1){
...
}else{
int a = num;
answer += a;
storey = storey - a;
}
}else if(num < 5){
...
}
...
}
1보다 크다면 다음 자릿수를 확인한다.
다음 자릿수가 5보다 크거나 같다면 5만큼 더하는 것이 최소횟수가 되고
55 => 60 => 100 => 0 / 총 5 + 4 + 1 = 10
65 => 70 => 100 => 0 / 총 5 + 3 + 1 = 9
5보다 작다면 5를 빼는 것이 최소횟수가 된다
45 => 40 => 0 / 총 5 + 4 = 9
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
...
}else if(num == 5){
if(size > 1){
int next = storey % 100;
next = (next - num) / 10;
if(next >= 5){
int a = num;
answer += a;
storey = storey + a;
}else{
int a = num;
answer += a;
storey = storey - a;
}
}else{
...
}
}else if(num < 5){
...
}
...
}
해당 자릿수에서의 최소 횟수를 구했다면 주어진 수의 1의 자리가 0이 되었으므로
주어진 수를 10으로 나누어 1의 자릿수를 없애준다.
그 후 반복문을 빠져나오면 구한 정답을 반환해준다.
while(storey != 0){
int size = Integer.toString(storey).length();
int num = storey % 10;
if(num > 5){
...
}else if(num == 5){
...
}else if(num < 5){
...
}
storey = storey / 10;
}
return answer;
}
자릿수가 5에 해당하는 경우를 생각해내지 못해 답을 찾아보게 되었다.