标签:none border tor cas clear 要求 one href efault
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
?(2)基本数字V ,L ,D 中的不论什么一个放在大数的右边採用相加的方式构成数目,仅仅能使用一个。
?(3)V 和X 左边的小数字仅仅能用I。
?(4)L 和C 左边的小数字仅仅能用X。
?(5)D 和M 左边的小数字仅仅能用C 。
?(6)在数字上加一横表示这个数字的1000倍。【因难以实现输入。临时忽略此要求】*/
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;
}
标签:none border tor cas clear 要求 one href efault
原文地址:https://www.cnblogs.com/mqxnongmin/p/10522166.html