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) {
int numtt[4] = {0};
int index = 0;
char numeral[4][2] = {'I', 'V', 'X', 'L', 'C','D', 'M', 'M'};
while (num)
{
numtt[index++] = num % 10;
num /= 10;
}
//此题主要是弄清楚罗马数字怎么表示数字
string str;
while (index--)
{
if (numtt[index] < 4)
{
while (numtt[index]--)
str.push_back(numeral[index][0]);
}
else if (numtt[index] == 4)
{
str.push_back(numeral[index][0]);
str.push_back(numeral[index][1]);
}
else if (numtt[index] < 9)
{
str.push_back(numeral[index][1]);
while (numtt[index] > 5)
{
str.push_back(numeral[index][0]);
--numtt[index];
}
}
else
{
str.push_back(numeral[index][0]);
str.push_back(numeral[index+1][0]);
}
}
return str;
}
};