프로그래머스 java 3진법 뒤집기

jieun·2022년 8월 15일
0

java 코테 공부

목록 보기
13/17

해결방법

  1. n을 3으로 나눈 나머지를 문자열에 앞에서부터 담기
  2. 3진법 문자열을 10진수로 변환

활용코드

n진법 -> 10진법 변환

Integer.parseInt(str, n);

전체코드

class Solution {
    public int solution(int n) {
        int answer = 0;
        String three = "";
        while(n>0) {
            three += n%3;
            n = n/3;
        }
        answer = Integer.parseInt(three, 3);
        return answer;
    }
}
profile
개발새발 블로그

0개의 댓글