2017-09-09 LeetCode_012 Integer to Roman

来源:互联网 发布:域名注册需要备案吗 编辑:程序博客网 时间:2024/05/19 17:02

12. Integer to Roman

Given an integer, convert it to a roman numeral.

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


solution:

class Solution {
2
public:
3
    string intToRoman(int num) {
4
        string m[4] = {"", "M", "MM", "MMM"},
5
            c[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
6
            x[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
7
            i[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
8
        return m[num/1000]+c[(num%1000)/100]+x[(num%100)/10]+i[num%10];
9
    }
10
};








原创粉丝点击