Pages

Saturday, September 19, 2015

The Wacmian Number

In the supposedly uninhabited Wacmahara Desert, a tribe of unusual people has been discovered. The Wacmians have only 2 fingers and a thumb on each hand, and have invented their own numbering system. The digits they use and the symbols they use for digits are quite unusual, but anthropologists have been able to represent them as follows:

%  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



 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
import java.util.*;
import java.lang.*;
import java.math.*;

public class P1 {
    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;
    }
}

No comments:

Post a Comment