[124カ国の数字-練習問題


// 124 나라의 숫자 - 연습문제
public class Country124 {

	// 방법 1
	public String solution(int n) {
		String answer = "";

		int mok = -1, na = 0;
		while (mok != 0) {
			na = n % 3;
			mok = n / 3;
			if (na == 0) {
				na = 3;
				mok -= 1;
			}
			answer = ((na == 3) ? Integer.toString(na + 1) : Integer.toString(na)) + answer;
			n = mok;
		}
		return answer;
	}

	// 방법 2
	public String solution1(int n) {
		String answer = "";

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

		while (n > 0) {

			answer = arr[n % 3] + answer;
			n = (n - 1) / 3;

		}

		return answer;
	}

	public static void main(String[] args) {

		Country124 s = new Country124();
		System.out.println(s.solution(1)); // 1
		System.out.println(s.solution(2)); // 2
		System.out.println(s.solution(3)); // 4
		System.out.println(s.solution(4)); // 11
		System.out.println(s.solution(5)); // 12
		System.out.println(s.solution(6)); // 14
		System.out.println(s.solution(7)); // 21
		System.out.println(s.solution1(8)); // 22
		System.out.println(s.solution1(9)); // 24
		System.out.println(s.solution1(10)); // 41
		System.out.println(s.solution1(11)); // 42
		System.out.println(s.solution1(12)); // 44
		System.out.println(s.solution1(13)); // 111
		System.out.println(s.solution1(14)); // 112
		System.out.println(s.solution1(15)); // 114
		System.out.println(s.solution1(16)); // 121

	}

}