LeetCode: Integer to Roman

来源:互联网 发布:现在淘宝代销好做吗 编辑:程序博客网 时间:2024/04/29 11:15

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;    }};


原创粉丝点击