Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
The idea is keep substracting value in the following table if the number is greater than the value.
I 1
IV 4
V 5
IX 9
X 10
XL 40
L 50
XC 90
C 100
CD 400
D 500
CM 900
M 1000
public enum romanChar {
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
int weight;
romanChar(int weight) {
this.weight = weight;
}
}
public String intToRoman(int num) {
if (num<=0||num>3999) {
return "";
}
StringBuilder result = new StringBuilder();
romanChar[] romans = romanChar.values();
for (int i=romans.length-1; i>=0; i--) {
while (num>=romans[i].weight) {
result.append(romans[i]);
num = num - romans[i].weight;
}
}
return result.toString();
}