public class Solution { public String intToRoman(int num) { int[] digits = new int[4]; int i = 0; String s = ""; digits[i] = num%10; while((num = num/10) > 0){ digits[++i] = num%10; } for(int j = 0; j <= i; j++){ if(j == 0){ switch (digits[j]) { case 1: s = s + "I"; break; case 2: s = s + "II"; break; case 3: s = s + "III"; break; case 4: s = s + "IV"; break; case 5: s = s + "V"; break; case 6: s = s + "VI"; break; case 7: s = s + "VII"; break; case 8: s = s + "VIII"; break; case 9: s = s + "IX"; break; default: break; } } else if(j == 1){ switch(digits[j]){ case 1: s = "X" + s; break; case 2: s = "XX" + s; break; case 3: s = "XXX" + s; break; case 4: s = "XL" + s; break; case 5: s = "L" + s; break; case 6: s = "LX" + s; break; case 7: s = "LXX" + s; break; case 8: s = "LXXX" + s; break; case 9: s = "XC" + s; break; default: break; } } else if(j == 2){ switch(digits[j]){ case 1: s = "C" + s; break; case 2: s = "CC" + s; break; case 3: s = "CCC" + s; break; case 4: s = "CD" + s; break; case 5: s = "D" + s; break; case 6: s = "DC" + s; break; case 7: s = "DCC" + s; break; case 8: s = "DCCC" + s; break; case 9: s = "CM" + s; break; default: break; } } else{ switch(digits[j]){ case 1: s = "M" + s; break; case 2: s = "MM" + s; break; case 3: s = "MMM" + s; break; default: break; } } } return s; } }
Jan 14 - Integer to Roman; String; Integer; Math;
原文:http://www.cnblogs.com/5683yue/p/5132215.html