[LeetCode] Integer to Roman

来源:互联网 发布:mysql子查询什么意思 编辑:程序博客网 时间:2024/06/18 10:43

Given an integer, convert it to a roman numeral.

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


class Solution {public:    string intToRoman(int num) {        int base[] = {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 res;        for(int i = 0; num > 0; i++)        {            int count = num / base[i];            num %= base[i];            while(count--)                res += symbol[i];        }        return res;    }};


0 0
原创粉丝点击