12. 整数转罗马数字

记录几个比较简洁的写法

class Solution {
public:
    string intToRoman(int num) {
        const std::string m[4][10] =
            {{"", "M", "MM", "MMM"},
             {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
             {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
             {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}};
        const int d[4] = {1000, 100, 10, 1};

        std::string roman;
        for (int i = 0; i < 4; ++i) {
            roman += m[i][num / d[i]];
            num %= d[i];
        }
        return roman;
    }
};
class Solution {
public:
    string intToRoman(int num) {
        int values[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        string reps[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        
        string response;
        for(int i = 0;i<13;i++)
        {
            while(num>=values[i])//循环保证可以处理多次需要同样的罗马数字,如:XX二十
            {
                num -= values[i];
                response += reps[i];
            }
        }
        return response;
    }
};
class Solution {
public:
    string intToRoman(int num) {
        string roman;
        while(num){
            if(num>=1000){num-=1000;roman+='M';}
            else{
                if(num>=900){num-=900;roman+='C';roman+='M';}
                else{
                    if(num>=500){num-=500;roman+='D';}
                    else{
                        if(num>=400){num-=400;roman+='C';roman+='D';}
                        else{
                            if(num>=100){num-=100;roman+='C';}
                            else{
                                if(num>=90){num-=90;roman+='X';roman+='C';}
                                else{
                                    if(num>=50){num-=50;roman+='L';}
                                    else{
                                        if(num>=40){num-=40;roman+='X';roman+='L';}
                                        else{
                                            if(num>=10){num-=10;roman+='X';}
                                            else{
                                                if(num>=9){num-=9;roman+='I';roman+='X';}
                                                else{
                                                    if(num>=5){num-=5;roman+='V';}
                                                    else{
                                                        if(num>=4){num-=4;roman+='I';roman+='V';}
                                                        else{num-=1;roman+='I';}
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return roman;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王蒟蒻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值