-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWacmianNumber.java
More file actions
73 lines (64 loc) · 1.58 KB
/
WacmianNumber.java
File metadata and controls
73 lines (64 loc) · 1.58 KB
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
65
66
67
68
69
70
71
72
73
import java.util.*;
import java.lang.*;
import java.math.*;
/*
% represents 0
) represents 1
~ represents 2
@ represents 3
? represents 4
\ represents 5
$ represents -1 (yes, they even have a negative digit)
As you may expect, their system is base 6 where each place value is 6 times the value to its right, as in the following examples:
)@% is 1*62+3*6+0 = 36+18+0 = 54
?$~~ is 4*63+(–1)*62+2*6+2 = 864–36+12+2 = 842
$~~ is (–1)*62+2*6+2 = –36+12+2 = -22
Your task is to take Wacmian numbers and represent them as standard base 10 numbers.
Sample Input
)@%
?$~~
$~~
%
#
Sample Output
54
842
-22
0
*/
public class WacmianNumber {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = scan.next();
while (line.charAt(0) != '#') {
System.out.println(convertNumber(line, 6));
line = scan.next();
}
}
public static int convertDigit(char wdigit) {
switch (wdigit) {
case '%':
return 0;
case ')':
return 1;
case '~':
return 2;
case '@':
return 3;
case '?':
return 4;
case '\\':
return 5;
case '$':
return -1;
}
return 0;
}
public static int convertNumber(String str, int base) {
int total = 0;
for (int x = 0; x < str.length(); x++) {
total = base * total + convertDigit(str.charAt(x));
}
return total;
}
}