1.Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Difficulty:Medium
1.这道题目其实是比较简单的,我们只要找到罗马数字的基本数字就行了,就好像阿拉伯数字的基本数字是0-9,其他数字都是由这十个数字组合而成的。找到这些罗马数字的基本数字之后我们就可以将整型数字用罗马数字表示了。因为题目中说明转换的范围时1到3999,因此罗马数字的基本数字可以用枚举的方法全部找出来。
code:
class Solution {
public:
string intToRoman(int num) {
string M[] = { "","M","MM","MMM" };
string C[] = { "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" };
string X[] = { "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" };
string I[] = { "","I","II","III","IV","V","VI","VII","VIII","IX" };
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[(num % 10)];
}
};