[Leetcode]-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.

思路:首先列出罗马数字4000以内的所有整点匹配串(3000-1000,900-100,90-10,9-1)

代码:

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


猜你喜欢

转载自blog.csdn.net/ljh0302/article/details/79056262