-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1199.java
64 lines (53 loc) · 1.65 KB
/
1199.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
55
56
57
58
59
60
61
62
63
64
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
long dec = 0;
String hex = "";
while (dec != -1) {
try {
dec = sc.nextLong();
if (dec == -1) break;
System.out.println("0x" + decToHex(dec));
} catch (Exception e) {
hex = sc.next();
System.out.println(hexToDec(hex.substring(2)));
}
}
}
public static String decToHex(long dec) {
ArrayList<String> hexList = new ArrayList<String>();
long resto;
String hex = "";
while (dec != 0) {
resto = dec % 16;
if (resto == 10) {
hexList.add("A");
} else if (resto == 11) {
hexList.add("B");
} else if (resto == 12) {
hexList.add("C");
} else if (resto == 13) {
hexList.add("D");
} else if (resto == 14) {
hexList.add("E");
} else if (resto == 15) {
hexList.add("F");
} else {
hexList.add(Long.toString(resto));
}
dec /= 16;
}
Collections.reverse(hexList);
for (String s : hexList) {
hex += s;
}
return hex;
}
public static Long hexToDec(String hex) {
return Long.parseLong(hex, 16);
}
}