Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
1,4,5,9为基本组成单位,从高位往下减,同时往字符串中添加对应的罗马字符。
string intToRoman(int num)
{
int values[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string romans[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result;
for (int i = 0; i < 13; i++)
{
while (num >= values[i])
{
num -= values[i];
result.append(romans[i]);
}
}
return result;
}