Nadesłany przez Tomasz Lubiński, 09 lutego 2007 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
Rzymskie.java:
//Konwersja liczby arabskie <-> rzymskie //www.algorytm.org //(c)2006 Tomasz Lubinski public class Rzymskie { private static final int arabic[] = {1000, 500, 100, 50, 10, 5, 1}; private static final char roman[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'}; private static final int ROMAN_N = arabic.length; //Converts arabic <number> to roman <result> //Returns <result> or "", if an ERROR occurs. public static String arabic2roman(int number) { int i = 0; //position in arabic and roman arrays String result = ""; if ((number > 3999) || (number <= 0)) { return result; } while ((number > 0) && (i < ROMAN_N)) { if(number >= arabic[i]) { number -= arabic[i]; result += roman[i]; } else if ((i%2 == 0) && (i<ROMAN_N-2) && // 9xx condition (number >= arabic[i] - arabic[i+2]) && (arabic[i+2] != arabic[i] - arabic[i+2])) { number -= arabic[i] - arabic[i+2]; result += roman[i+2]; result += roman[i]; i++; } else if ((i%2 == 1) && (i<ROMAN_N-1) && //4xx condition (number >= arabic[i] - arabic[i+1]) && (arabic[i+1] != arabic[i] - arabic[i+1])) { number -= arabic[i] - arabic[i+1]; result += roman[i+1]; result += roman[i]; i++; } else { i++; } } return result; } //Converts roman <number> to arabic //Returns <result> or -1, if an ERROR occurs. public static int roman2arabic(String number) { int i = 0, //position in arabic and roman arrays j = 0, //position in number result = 0, length = 0; length = number.length(); while ((j<length) && (i<ROMAN_N)) { if(number.charAt(j) == roman[i]) { result += arabic[i]; j++; } else if ((i%2 == 0) && (i<ROMAN_N-2) && // 9xx condition (j<length-1) && (number.charAt(j) == roman[i+2]) && (number.charAt(j+1) == roman[i])) { result += arabic[i] - arabic[i+2]; j += 2; i++; } else if ((i%2 == 1) && (i<ROMAN_N-1) && //4xx condition (j<length-1) && (number.charAt(j) == roman[i+1]) && (number.charAt(j+1) == roman[i])) { result += arabic[i] - arabic[i+1]; j += 2; i++; } else { i++; } } //there was an error during conversion if (i == ROMAN_N) { result = -1; } return result; } /** * @param args */ public static void main(String[] args) { String roman; roman = arabic2roman(1981); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(1); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(3); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(4); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(5); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(6); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(45); System.out.println(roman2arabic(roman) + " = " + roman); roman = arabic2roman(68); System.out.println(roman2arabic(roman) + " = " + roman); } }