java 프로그래머스 [Level 1] _3진법 뒤집기
2021. 4. 30. 17:55ㆍ프로그래머스 알고리즘/코딩 테스트 문제
반응형
출제 링크 : programmers.co.kr/learn/courses/30/lessons/68935
import java.util.Stack;
class Solution {
public int solution(int n) {
int answer = 0;
Stack<Integer> temp = new Stack<Integer>();
while (n >= 3) {
temp.add(n % 3);
n = n / 3;
}
temp.add(n);
// System.out.println(temp);
int index = 0;
while (!temp.isEmpty()) {
int dammy = Integer.parseInt(String.valueOf(temp.pop()));
// System.out.println("dammy : " + dammy );
if (index == 0) {
answer += dammy;
} else if (index == 1) {
answer += (dammy * 3);
} else {
int in = (int) Math.pow(3, index);
answer += dammy * in;
}
index++;
}
return answer;
}
}
반응형