12. Integer to Roman

来源:互联网 发布:php怎么读 编辑:程序博客网 时间:2024/06/18 13:40

题目

Given an integer, convert it to a roman numeral.

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

分析

构造一个4*10的矩阵,保存1-9,10-90,100-900,1000-3000的罗马字符,然后对num从千位开始找到合适的罗马字符与之组合即可。

class Solution {public:    string intToRoman(int num) {        char* 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"}        };        string roman;        roman.append(c[3][num / 1000 % 10]);        roman.append(c[2][num / 100 % 10]);        roman.append(c[1][num / 10 % 10]);        roman.append(c[0][num % 10]);                 return roman;    }};


0 0
原创粉丝点击