Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Given a positive integer convert it in to its Roman number’s representation.
Example 1:
Input:
num = 3
Output:
"III"
Explanation:
3 is represented as 3 ones.
Example 2:
Input:
num = 58
Output:
"LVIII"
Explanation:
L = 50, V = 5, III = 3.
Example 3:
Input:
num = 1994
Output:
"MCMXCIV"
Explanation:
M = 1000, CM = 900, XC = 90 and IV = 4.
Solution
public string IntToRoman(int num) {
string[] thousands = {"", "M", "MM", "MMM"};
string[] hundreds = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string[] tens = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string[] ones = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return thousands[num / 1000] +
hundreds[(num % 1000) / 100] +
tens[(num % 100) / 10] +
ones[num % 10];
}