코테/프로그래머스

3진법 뒤집기

밍래그로프 2020. 12. 6. 22:37

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public int solution(int n) {
        int answer = 0;
        int num = n;
        String an ="";
        while(num > 0){
            an += num%3;  
            num /= 3;
        }
        // System.out.println(an);
        String[] ten = an.split("");
        for(int i = ten.length , j = 1 ; i > 0 ; i--){
            num = Integer.parseInt(ten[i-1]);
            //num 재사용. ! 과연 재사용이 좋을까 새로운 변수를 만드는게 좋을까
            answer = answer + num * j;
            j *= 3;
        }
        return answer;
    }
}
cs

System.out.println(an);</span></div><div