-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlphaCentauri_66859.java
54 lines (50 loc) · 1.7 KB
/
AlphaCentauri_66859.java
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
47
48
49
50
51
52
53
54
import java.util.Map;
import java.util.Scanner;
public class AlphaCentauri_66859 {
private static final Map<Integer, String> numbers = Map.ofEntries(
Map.entry(1, "1"),
Map.entry(2, "2"),
Map.entry(3, "3"),
Map.entry(4, "4"),
Map.entry(5, "5"),
Map.entry(6, "6"),
Map.entry(7, "7"),
Map.entry(8, "8"),
Map.entry(9, "9"),
Map.entry(10, "A"),
Map.entry(11, "B"),
Map.entry(12, "C"),
Map.entry(13, "D"),
Map.entry(14, "E"),
Map.entry(15, "F"));
public static void main(String... args) {
Scanner scanner = new Scanner(System.in);
String[] input = scanner.nextLine().split(" ");
System.out.println(radixConvert(Integer.parseInt(input[0]), Integer.parseInt(input[1])));
scanner.close();
}
private static String radixConvert(int input, int radix) {
if (input < radix) {
if (radix <= 10)
return String.valueOf(input);
else
return numbers.get(input);
} else {
StringBuilder builder = new StringBuilder();
int divisor = input;
while (divisor > 0) {
if (divisor < radix) {
builder.append(divisor);
break;
}
int remainder = divisor % radix;
if (remainder >= 10)
builder.append(numbers.get(remainder));
else
builder.append(remainder);
divisor /= radix;
}
return builder.reverse().toString();
}
}
}