美文网首页
12. Integer to Roman

12. Integer to Roman

作者: 林里icer | 来源:发表于2018-04-09 21:50 被阅读0次

整数转罗马数字,13题的镜像问题,用贪心,每次找最大的值

string intToRoman(int num) {
        string symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};    
        int value[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; 
        int i=0;
        string res = "";
        while(num!=0){
            if(num>=value[i]){
                res+=symbol[i];
                num-=value[i];
            }
            else i++;    
        }
        return res;
    }

相关文章

网友评论

      本文标题:12. Integer to Roman

      本文链接:https://www.haomeiwen.com/subject/ywdwhftx.html