[LeetCode] Integer to Roman

来源:互联网 发布:网页美工设计视频 编辑:程序博客网 时间:2024/06/06 20:01
[Problem]

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


[Solution]

class Solution {
public:
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

//1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
string roman[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int integer[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string res = "";
while(num > 0){

for(int i = 0; i < 13; ++i){
if(num >= integer[i]){
while(num >= integer[i]){
res.append(roman[i]);
num -= integer[i];
}
break;
}
}
}
return res;
}
};


 说明:版权所有,转载请注明出处。Coder007的博客
原创粉丝点击