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) {
final int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5,
4, 1 };
final String[] symbol = { "M", "CM", "D", "CD", "C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I" };
StringBuilder result = new StringBuilder();
for (int i = 0; num > 0; i++) {
int count = num / values[i];
num %= values[i];
for (; count > 0; count--) {
result.append(symbol[i]);
}
}
return new String(result);
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。