본문 바로가기
휴지통/알고리즘 & 자료구조

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));
   }

}

'휴지통 > 알고리즘 & 자료구조' 카테고리의 다른 글

백준 2525  (0) 2022.06.26
백준 24416  (0) 2022.06.26
멀쩡한 사각형  (0) 2022.06.23
백준 7569  (0) 2022.06.22
백준 1012  (0) 2022.06.20