Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question
(1)前置条件为 1<=num <= 3999
(2)取出每一位,得到对应罗马字符
class Solution {
public:
string intToRoman(int num) {
if(1> num || num > 3999)
return "";
string roMan[4][10] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
string temp = "";
int row = 0;
while(0 != num)
{
temp = roMan[row][num%10] + temp;
num = num / 10;
row++;
}
return temp;
}
};