码迷,mamicode.com
首页 > 其他好文 > 详细

Integer to Roman

时间:2016-01-03 22:12:41      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

Given an integer, convert it to a roman numeral.

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

 

Subscribe to see which companies asked this question

看了下百度百科的罗马数字。这个应该可以穷举吧

class Solution {
public:
    string intToRoman(int num) {
        string c[4][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},
                       {"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
                       {"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
                       {"0","M","MM","MMM"}};
        string res = "";
        if (num <= 0) {
            return res;
        }


        if (num / 1000 != 0) {
            res += c[3][num / 1000];
        }
        
        if (num % 1000 / 100 != 0) {
            res += c[2][num % 1000 / 100];
        }

        if (num % 100 / 10 != 0) {
            res += c[1][num % 100 / 10];
        }

        if (num % 10 != 0) {
            res += c[0][num % 10];
        }
        
        return res;
    }
};

 

Integer to Roman

标签:

原文地址:http://www.cnblogs.com/SpeakSoftlyLove/p/5097138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!