Integer to Roman

来源:互联网 发布:北极熊团 知乎 编辑:程序博客网 时间:2024/06/03 17:05

Integer to Roman

Given an integer, convert it to a roman numeral.

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


对int值的每一位进行转换,首先建立映射表,再一一进行转换。

class Solution {public:string intToRoman(int num) {string romans[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 result;int count = 0;while (num>0){int v = num%10;num /= 10;result = romans[count++][v]+result;}return result;}};


0 0
原创粉丝点击