Level1) 3진법 뒤집기
2021. 4. 28. 23:06ㆍPS/programmers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class Level1_3진법뒤집기 { public static int solution(int n) { int answer = 0; // 10진법 -> 3진법 뒤집은거 StringBuilder reverseThreeRadix = new StringBuilder(); while (n > 0) { reverseThreeRadix.append(n % 3); n /= 3; } // 3진법 -> 10진법 long before10 = Long.parseLong(reverseThreeRadix.toString()); int count = 0; while (before10 > 0) { long index = before10 % 10; answer += index * (int) Math.pow(3, count++); before10 /= 10; } return answer; } public static void main(String[] args) { int n = 45; System.out.println(solution(n)); } } | cs |
'PS > programmers' 카테고리의 다른 글
Level1) 소수구하기 (0) | 2021.04.30 |
---|---|
Level1) 신규아이디추천 (0) | 2021.04.29 |
Level1) 키패드 누르기 (0) | 2021.04.27 |
Level1) 다트게임 (0) | 2021.04.27 |
Level1) 비밀지도 (0) | 2021.03.17 |