Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
模拟题,主要是搞清楚罗马数字的写法和规则,然后打个表就可以了。
代码语言:javascript复制class Solution {
public:
string intToRoman(int num)
{
string table[4][11]={
{"", "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 result = "";
int now=0;
while (num > 0)
{
result=table[now][num] result;
num/=10;
now ;
}
return result;
}
};