题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
难度: medium思路:
找到罗马数字对应阿拉伯数字的规律即可。将数字对应序列从大到小排列,每次减掉可以减掉的最大数,换成对应的罗马字母即可
代码如下:
class Solution {
public:
string intToRoman(int num) {
int values[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
string rValue[] = {"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+=rValue[i];
}
}
return result;
}
};