首页 > 其他 > 详细

Leetcode 12. Integer to Roman

时间:2019-04-26 14:02:34      阅读:98      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/integer-to-roman/submissions/

using namespace std;
class Solution {
public:
    string reps="IVXLCDM";
    
    string itor(int k,int l){
        switch(k){
            case 0: 
            case 1: 
            case 2: 
            case 3: return string(k,reps[l]);
            case 4: return reps[l]+string(1,reps[l+1]);
            case 5: return string(1,reps[l+1]);
            case 6: 
            case 7:
            case 8: return reps[l+1]+string((k%5),reps[l]);
            case 9: return reps[l]+string(1,reps[l+2]);
        }
        return "";
    }
    string intToRoman(int num) {
        string ans;
        ans+=itor(num/1000,6),num%=1000;
        ans+=itor(num/100,4),num%=100;
        ans+=itor(num/10,2),num%=10;
        ans+=itor(num,0);
        return ans;
    }
};

Leetcode 12. Integer to Roman

原文:https://www.cnblogs.com/ximelon/p/10773874.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!