12. Integer to Roman

来源:互联网 发布:大家的日语 知乎 编辑:程序博客网 时间:2024/05/05 10:16

Given an integer, convert it to a roman numeral.

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

思路:任何一种计数法都是基于一些base,每个base代表一个scale的数字,然后累加就可以了。

http://bangbingsyb.blogspot.jp/2014/11/leetcode-integer-to-roman.html

class Solution {public:    string intToRoman(int num) {         string dict[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};         int val[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};         string ans;         int count;         for(int i = 0; i < 13; i++)         {             if(num >= val[i])             {                count = num/val[i];                num = num % val[i];                for(int j = 0; j < count; j++)                {                    ans.append(dict[i]);                }             }         }         return ans;    }};
0 0