LeetCode OJ 之 Integer to Roman (整数转化为罗马数字)

来源:互联网 发布:淘宝上如何收藏店铺 编辑:程序博客网 时间:2024/06/05 02:00

题目:

Given an integer, convert it to a roman numeral.

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

给一个整型数,把他转化成罗马数字。输入范围从1 到 3999。

思路:

参看罗马数字转化为整数:http://blog.csdn.net/u012243115/article/details/40820559 。

代码:

class Solution {public:    string intToRoman(int num)     {        int radix[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};        string symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};        string roman;        for(size_t i = 0 ; num > 0 ; i++)        {            int count = num / radix[i];            num %= radix[i];            for( ; count > 0 ; --count)            {                roman += symbol[i];            }        }        return roman;    }    };


0 0