Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
string intToRoman(int num) {
char table[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
int i = 0;
int index = 0;
int bit = 0;
string ret;
for (i = 0; i < 4; i++) {
bit = num % 10;
switch(bit) {
case 3:
ret.insert(0, 1, table[index]);
case 2:
ret.insert(0, 1, table[index]);
case 1:
ret.insert(0, 1, table[index]);
break;
case 4:
ret.insert(0, 1, table[index+1]);
ret.insert(0, 1, table[index]);
break;
case 8:
ret.insert(0, 1, table[index]);
case 7:
ret.insert(0, 1, table[index]);
case 6:
ret.insert(0, 1, table[index]);
case 5:
ret.insert(0, 1, table[index+1]);
break;
case 9:
ret.insert(0, 1, table[index+2]);
ret.insert(0, 1, table[index]);
break;
}
index += 2;
num /= 10;
if (num == 0) break;
}
return ret;
}
};