Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
» Solve this problem
定义常量数组直接转换。
class Solution {
public:
const string romans[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 intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string ans = "";
int idx = 0;
while (num != 0) {
ans = romans[idx][num % 10] + ans;
num /= 10;
idx++;
}
return ans;
}
};