美文网首页Leetcode
LeetCode #12 整数转罗马数字

LeetCode #12 整数转罗马数字

作者: HU兔兔 | 来源:发表于2020-02-06 11:39 被阅读0次
    class Solution {
    public:
        string intToRoman(int num) {
             string Roma="IVXLCDM##";
             int i=6;
             int j=1000;
             int n,m;
             string ans;
             while(j){
                 n=(num/j)%10;
                 switch(n){
                     case 4:
                     ans.push_back(Roma[i]);
                     ans.push_back(Roma[i+1]);
                     break;
                     case 9:
                     ans.push_back(Roma[i]);
                     ans.push_back(Roma[i+2]);
                     break;
                     default:
                     if(n/5){
                         ans+=Roma[i+1];
                     }
                     for(m=1;m<=n%5;m++){
                         ans+=Roma[i];
                     }
                 }
                 j/=10;
                 i-=2;
             }
             return ans;
        }
    };
    

    leetcode的计时是真滴有毒

    相关文章

      网友评论

        本文标题:LeetCode #12 整数转罗马数字

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