public class Solution012 { public static final String[][] base = { { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }, { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }, { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" }, { "", "M", "MM", "MMM" } }; public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); sb.append(base[3][(num / 1000) % 10]); sb.append(base[2][(num / 100) % 10]); sb.append(base[1][(num / 10) % 10]); sb.append(base[0][(num % 10)]); return sb.toString(); } public static void main(String[] args) { Solution012 s12 = new Solution012(); int xs[] = { 1, 4, 6, 7, 9, 18, 19, 99, 3999 }; for (int x : xs) { System.out.println(x + "-->" + s12.intToRoman(x)); } } }
|