Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
题目这样出就有点坑了,我一开始根本不知道罗马数字什么1000的怎么表示,甚至都不知罗马数字还有1000...
不说了,其实挺简单了,就是把输入的数字用罗马数字表示出来,罗马数字用字符串表示,代码如下:
class Solution {
public:
string intToRoman(int num) {
string romans[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
int nums[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
string s("");
int pos = 12;
while(pos >= 0){
if(num / nums[pos]){
s += romans[pos];
num -= nums[pos];
}
else
--pos;
}
return s;
}
};