알고리즘/프로그래머스

[프로그래머스] 124 나라의 숫자

하빈H 2022. 9. 3. 00:57

https://school.programmers.co.kr/learn/courses/30/lessons/12899

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1, 2, 4만을 사용해서 만든 숫자로 변환할 때도 진법 변환할 때 흔히 사용하는 나머지를 이용하면 된다.

 

1 % 3 = 1

2 % 3 = 2

3 % 3 = 0

 

package programmerstest.level2;

public class Country124 {
    
    public static String solution(int n) {
        String answer = "";

        String[] substitution = {"4", "1", "2"};

        while(n != 0) {

            int temp = n % 3;

            answer = substitution[temp] + answer;
            n /= 3;

            if(temp == 0) {
                n--;
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        System.out.println(solution(1));
        System.out.println(solution(2));
        System.out.println(solution(3));
        System.out.println(solution(4));
        System.out.println(solution(5));
        System.out.println(solution(6));
        System.out.println(solution(7));
        System.out.println(solution(8));
        System.out.println(solution(9));
        System.out.println(solution(10));
    }
}