您的位置:首页 > 其它

LeetCode: Integer to Roman

2012-10-07 14:49 253 查看
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 roman(int num, char ten, char five, char one)
{
string str = "";
if (num == 9)
{
str += one;
str += ten;
}
else if (num >= 5)
{
str += five;
while(num-- > 5)
str += one;
}
else if (num == 4)
{
str += one;
str += five;
}
else
{
while(num-- > 0)
str += one;
}
return str;
}

string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string result = "";
result += roman(num/1000%10, 0, 0, 'M');
result += roman(num/100%10, 'M', 'D', 'C');
result += roman(num/10%10, 'C', 'L', 'X');
result += roman(num%10, 'X', 'V', 'I');
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: