Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
之前写过将罗马数转化为整数,现在回顾一下罗马数的写法
I -> 1
V -> 5
X->10
L-> 50
C->100
D->500
M->1000
因为题目给出的范围是1到3999,所以讲千位数1-3,百位数1-9,十位数1-9,个位数1-9的写法列出,然后将int分解对应即可。
class Solution {
public:
string intToRoman(int num) {
string table[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 result;
int count = 0;
while(num > 0){
int temp = num % 10;
result = table[count][temp] + result;
num /= 10;
count++;
}
return result;
}
};