일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- Spring Security
- hanghae99
- web
- Anolog
- DI
- jQuery
- server send event
- SseEmitter
- 항해99
- html
- JWT
- Stream
- real time web
- jenkins
- google oauth
- spring
- oauth
- programmers
- python
- Hibernate
- 생명주기 콜백
- flask
- cookie
- WIL
- session
- Java
- bean
- JPA
- javascript
- Project
Archives
- Today
- Total
끄적끄적 코딩일지
[Programmers]3진법 뒤집기(난이도:★★★★) 본문
https://programmers.co.kr/learn/courses/30/lessons/68935
코딩테스트 연습 - 3진법 뒤집기
자연수 n이 매개변수로 주어집니다. n을 3진법 상에서 앞뒤로 뒤집은 후, 이를 다시 10진법으로 표현한 수를 return 하도록 solution 함수를 완성해주세요. 제한사항 n은 1 이상 100,000,000 이하인 자연수
programmers.co.kr
class Solution {
public int solution(int n) {
long a = change((long)n);
a = reverseNumber(a);
int answer = changeTen(a);
return answer;
}
// 3진법 - > 10진법
public int changeTen(long num) {
long tmp = num;
int idx = 1;
int res = 0;
while(tmp > 0) {
long s = (int) (tmp % 10L) ;
s = s * idx;
res += s;
idx *= 3;
tmp /= 10L;
}
return res;
}
// 10진법 -> 3진법
public long change(long n){
long cul = n;
long res = 0L;
long dis = 1L;
while(cul > 0){
long tmp = (cul % 3L) * dis;
dis *= 10L;
cul = cul / 3L;
res += tmp;
}
return res;
}
// 숫자 뒤집기
public long reverseNumber(long num) {
long tmp = num;
long res = 0L;
long count = Long.toString(num).length();
long idx = (long)Math.pow(10, count-1L);
while(tmp > 0) {
long s = (tmp % 10L);
res += (s * idx);
idx /= 10L;
tmp /= 10L;
}
return res;
}
}
'알고리즘' 카테고리의 다른 글
[Programmers]같은 숫자는 싫어(난이도:★★★★) (0) | 2022.05.16 |
---|---|
[Programmers]최소 직사각형(난이도:★★★★) (0) | 2022.05.16 |
[Programmers]하샤드 수(난이도:★★★) (0) | 2022.05.16 |
[Programmers]콜라츠 추측(난이도:★★★) (0) | 2022.05.16 |
[Programmers]제일 작은수 제거하기(난이도:★★★) (0) | 2022.05.16 |