일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- flask
- WIL
- JPA
- DI
- Java
- Hibernate
- programmers
- javascript
- cookie
- session
- Anolog
- spring
- SseEmitter
- jQuery
- oauth
- 항해99
- server send event
- 생명주기 콜백
- web
- Spring Security
- hanghae99
- real time web
- html
- bean
- Stream
- google oauth
- Project
- JWT
- jenkins
- python
Archives
- Today
- Total
끄적끄적 코딩일지
[Programmers]3진법 뒤집기(난이도:★★★★) 본문
https://programmers.co.kr/learn/courses/30/lessons/68935
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 |