[프로그래머스/LV2] [3차] n진수 게임 - 비트 && 진법 (Java)
[프로그래머스/LV2] n진수 게임 문제를 Java로 해결한 풀이입니다. 진법을 활용한 풀이로 설명합니다.
[프로그래머스/LV2] [3차] n진수 게임
🙋♂️ 들어가며
우선 나는 전체 나올 수 있는 가지수를 고려해 (t*m) 즉 라운드 횟수 * 사람수 만큼 곱해서 거기서 필요한 사람의 요소만큼 추출하면 되겠다.
그리고 각 진수별로 예외처리를 해주면 되겠다.
참고로 JAVA에서는 StringBuilder와 sb.reverse().toString() 을 활용하면 비트 구현하는데 큰 지장은 없을 것이다.
✅ 정답 코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public String solution(int n, int t, int m, int p) {
String answer = "";
String temp = "";
int num = 0;
while (num < (m*t) + 1) {
StringBuilder sb = new StringBuilder();
int temp_num = num;
if (temp_num == 0) {
sb.append(num);
}
while (temp_num > 0) {
int remain = temp_num % n;
if (remain == 10) sb.append("A");
else if (remain == 11) sb.append("B");
else if (remain == 12) sb.append("C");
else if (remain == 13) sb.append("D");
else if (remain == 14) sb.append("E");
else if (remain == 15) sb.append("F");
else sb.append(remain);
temp_num /= n;
}
// 추가
temp += sb.reverse().toString();
// 다음으로 전환
num++;
}
int idx = p-1;
int cnt = 0;
while (cnt < t) {
int index = idx + (cnt * m);
char ch = temp.charAt(index);
answer += ch;
cnt++;
}
return answer;
}
}
This post is licensed under CC BY 4.0 by the author.
