programmers 81301 - 숫자 문자열과 영단어

문제 분석

  • 문제 유형 : 진법 변환
  • 걸린 시간 :
  • 풀이 전략 :
  • 막힌 부분 : 테스트 케이스 일부가 통과하지 않는다.

나의 풀이

class Solution {
    public int solution(int n) {
        int ternary = toTernary(n);
        System.out.println(ternary);
        System.out.println(toDecimal(ternary));
        return toDecimal(ternary);
    }
    
    private int toTernary(int n) {
        int result = 0;
        while (true) {
            if (n == 0) break;
            result *= 10;
            result += n % 3;
            n /= 3;
        }
        return result;
    }
    
    private int toDecimal(int n) {
        int result = 0;
        int plus = 1;
        while (true) {
            if (n == 0) break;
            if (plus == 1) {
                result += n % 10; 
            } else {
                result += (n % 10) * (plus);
            }
            n /= 10;
            plus *= 3;
        }
        return result;
    }
}