Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
public String intToRoman(int num) { int digit; String str = ""; int n = 10; while(num > 0){ digit = num % n; switch(n){ case 10: switch(digit){ case 1: str = "I" + str; break; case 2: str = "II" + str; break; case 3: str = "III" + str; break; case 4: str = "IV" + str; break; case 5: str = "V" + str; break; case 6: str = "VI" + str; break; case 7: str = "VII" + str; break; case 8: str = "VIII" + str; break; case 9: str = "IX" + str; break; default: break; } break; case 100: digit = digit / 10; switch(digit){ case 1: str = "X" + str; break; case 2: str = "XX" + str; break; case 3: str = "XXX" + str; break; case 4: str = "XL" + str; break; case 5: str = "L" + str; break; case 6: str = "LX" + str; break; case 7: str = "LXX" + str; break; case 8: str = "LXXX" + str; break; case 9: str = "XC" + str; break; default :break; } break; case 1000: digit = digit / 100; switch(digit){ case 1: str = "C" + str; break; case 2: str = "CC" + str; break; case 3: str = "CCC" + str; break; case 4: str = "CD" + str; break; case 5: str = "D" + str; break; case 6: str = "DC" + str; break; case 7: str = "DCC" + str; break; case 8: str = "DCCC" + str; break; case 9: str = "CM" + str; break; default : break; } break; case 10000: digit = digit / 1000; switch(digit){ case 1: str = "M" + str; break; case 2: str = "MM" + str; break; case 3: str = "MMM" + str; break; } break; } num = num - digit * n/10; n = n * 10; } return str; }
Integer to Roman,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u010378705/article/details/31744511