본문 바로가기
알고리즘 & 자료구조/프로그래머스

124 나라의 숫자

by 신재권 2022. 6. 24.
package programmers;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class OneToFourNumberOfCountry {

   public String solution(int n) {
      String answer = "";
      int i = 0;
      while(0 < n) {
         i = n%3;

         if(0 == i) {
            n = n/3 -1;
            i = 4;
         }else n /= 3;

         if (i == 1) {
            answer = "1" + answer;
         }
         if (i == 2) {
            answer = "2" + answer;
         }
         if (i == 4) {
            answer = "4" + answer;
         }
      }

      return answer;
   }

   @Test
   void test() {
      assertEquals("1", solution(1));
      assertEquals("2", solution(2));
      assertEquals("4", solution(3));
      assertEquals("11", solution(4));
   }

}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

시저암호  (0) 2022.10.20
예산  (0) 2022.10.20
멀쩡한 사각형  (0) 2022.06.23
내적  (0) 2022.04.16
음양 더하기  (0) 2022.04.15