Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class RomanNumerals {
- // Holds expanded roman character representations of each digit, for the first four places
- static String[][] lookupTable = new String[4][10];
- static void initLookupTable() {
- // Roman representation of One, Five, Ten for each place
- String[][] romanChars = {
- {"I", "V", "X"}, // ---X ones
- {"X", "L", "C"}, // --X- tens
- {"C", "D", "M"}, // -X-- hundreds
- {"M", "?", "?"} // X--- thousands
- };
- // Compute lookup table for each of 4 places (ones, tens, hundreds, thousands)
- for (int i = 0; i < 4; i++) {
- String one = romanChars[i][0];
- String five = romanChars[i][1];
- String ten = romanChars[i][2];
- lookupTable[i][0] = "";
- lookupTable[i][1] = one;
- lookupTable[i][2] = one + one;
- lookupTable[i][3] = one + one + one;
- lookupTable[i][4] = one + five;
- lookupTable[i][5] = five;
- lookupTable[i][6] = five + one;
- lookupTable[i][7] = five + one + one;
- lookupTable[i][8] = five + one + one + one;
- lookupTable[i][9] = one + ten;
- }
- }
- // Initialize the lookup table when the class loads
- static { initLookupTable(); }
- // Main; run tests, then interactively convert numerals
- public static void main(String[] args) throws IOException {
- testRomanNumerals();
- interactiveRomanNumerals();
- }
- // Read numbers from stdin, output roman numeral representation of input to stdout
- private static void interactiveRomanNumerals() throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String input;
- String output;
- while ((input = br.readLine()) != null && input.length() > 0) {
- System.out.println(toRomanNumerals(input));
- }
- }
- /**
- * Converts a number, as an input string, into the roman numeral representation
- * @param input a 0-4 digit number, represented as a string
- * @return the roman numeral representation of the input
- */
- private static String toRomanNumerals(String input) {
- String output;
- // Reverse string order, it's easier to think of the "places" this way. index 0 == ones, 1 == tens, etc.
- StringBuffer data = new StringBuffer(input).reverse();
- output = "";
- for (int i = 0; i < data.length(); i++) {
- // For each char in the input, get the Roman representation, prepend it to the output string (to reverse the order again)
- output = getRomanNumeral(data.charAt(i), i) + output;
- }
- return output;
- }
- /**
- * @param value one digit of a number, as a character (eg: '1')
- * @param placeIdx the digit's "place" (0 == ones, 1 == tens, 2 == hundreds, 3 == thousands)
- * @return the roman numeral representation of the one input digit
- */
- private static String getRomanNumeral(char value, int placeIdx) {
- return lookupTable[placeIdx][value - '0'];
- }
- private static void testRomanNumerals() {
- testEquals("I", toRomanNumerals("1"));
- testEquals("VI", toRomanNumerals("6"));
- testEquals("XXXI", toRomanNumerals("31"));
- testEquals("XXIV", toRomanNumerals("24"));
- testEquals("L", toRomanNumerals("50"));
- testEquals("CCCLXIX", toRomanNumerals("369"));
- testEquals("CD", toRomanNumerals("400"));
- testEquals("CDXLVIII", toRomanNumerals("448"));
- testEquals("MCMXCVIII", toRomanNumerals("1998"));
- testEquals("MMDCCLI", toRomanNumerals("2751"));
- System.out.println("All test cases passed.");
- }
- private static void testEquals(String left, String right) {
- if (!left.equals(right))
- throw new RuntimeException("Test failed. left != right (" + left + " != " + right + ")");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment