알고리즘 풀이 방법입니다.
문제(Problem) -> 생각(Think) -> 해결책(Solution) -> 리뷰(Review) 를 통해서 정리해서 작성합니다.
Problem📄
https://school.programmers.co.kr/learn/courses/30/lessons/68935
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
Think🤔
10진수 -> 3진법
3진수 -> 10진법
으로 변환
진법 구하는 공식 참조 , 변환 시 reverse String이용
class Solution {
public int solution(int n) {
int answer = 0;
String str = "";
while(n>0){
str += (n%3);
n = n / 3;
}
System.out.println(str);
int exp = 0;
// 거꾸로 반복문해야함
for(int i=str.length()-1; i>0; i--){
// 10진수로 바꾸기
answer += Integer.parseInt(String.valueOf(str.charAt(i))) * Math.pow(3,exp);
exp++;
}
return answer;
}
}
Solution✍
class Solution {
public int solution(int n) {
int answer = 0;
String str = "";
// 3진법 변환
while(n > 0){
str += n % 3;
n = n / 3;
}
System.out.println(str);
int digit = str.length() - 1;
// 10진법 변환
for(int i=0; i<str.length(); i++){
answer += Integer.parseInt(String.valueOf(str.charAt(i))) * Math.pow(3, digit);
digit--;
}
return answer;
}
}
digit이라는 자릿수 변수를 놓고 인덱스 위치는 -1이 필요함
진법 변환하는 방법은 나머지를 이용해서 3진법으로 변환 시키고,
다시 10진법으로 변환 시킬때 , 예제를 보면서 규칙을 구하고 그 규칙을 코드로 작성
Review🤩
규칙 찾는게 오래걸렸음 ㅡ,.ㅡ
'Algorithm' 카테고리의 다른 글
[프로그래머스] 실패율 (0) | 2022.08.08 |
---|---|
[프로그래머스] K번째수 (0) | 2022.08.02 |
[프로그래머스] 두 개 뽑아서 더하기 (0) | 2022.07.28 |
[프로그래머스] 2016년 (0) | 2022.07.27 |
[프로그래머스] 최소직사각형 (0) | 2022.07.27 |