매일 Algorithm

신재원·2023년 1월 13일
0

Algorithm

목록 보기
5/243

프로그래머스 : 3진법 뒤집기 (LEVEL 1)

class Solution {
    // 3진법으로 변환후 10진법으로 변환
    public int solution(int n) {
        /*
        N진법을 -> 10진법으로
        Integer.parseInt(i,N);
        */
        int answer;
        String result = "";
        // 45
        // while문을 통해 3진법
        while (n != 0) {
            result += n % 3;
            n /= 3;
        }
        // 0021이 담긴 String 에서 10진법으로 변환
        answer = Integer.parseInt(result, 3);

        return answer;
    }
}

0개의 댓글