반응형
사실 중간에 순서 뒤집기 때문에..
그냥 단순히 나눈 순서대로 Math.pow() 해도 될텐데
진법 변환을 다 해보았다..
n진법 변환시 나머지를 어떻게 쌓는지 순서를 파악하면 간단할듯.
import java.util.*;
class Solution {
public int solution(int n) {
return getNumFormatTen(getNumFormatThree(n));
}
public String getNumFormatThree(int n){
Deque<String> deque = new ArrayDeque<>();
while(true){
deque.offer(Integer.toString(n % 3));
int share = n / 3;
if(share == 0){
break;
}else{
n = share;
}
}
StringBuilder formatThree = new StringBuilder();
while (!deque.isEmpty()) {
formatThree.append(deque.poll()); //넣은 순 꺼내기.
}
return formatThree.toString();
}
public int getNumFormatTen(String n){
int num=0;
for(int i=0;i<n.length();i++){
num += Math.pow(3,i) * (n.charAt(n.length()-1-i)-'0');
}
return num;
}
}
반응형
'코딩 관련 > 코딩문제풀기' 카테고리의 다른 글
[프로그래머스] Lv1. 가장 많이 받은 선물 (0) | 2024.10.23 |
---|---|
[프로그래머스] Lv1. 모의고사 (0) | 2024.10.23 |
[프로그래머스] 소수 만들기 (0) | 2024.10.23 |
[프로그래머스] 로또의 최고 순위와 최저 순위 (0) | 2024.10.23 |
[프로그래머스] 신규 아이디 추천 (0) | 2024.10.22 |