12. Integer to Roman \ 13. Roman to Integer

本文介绍了两种实用的C++类实现,一种用于将整数转换为罗马数字,另一种用于将罗马数字转换回整数。提供了详细的代码示例,包括使用预定义的罗马数字和整数映射表的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Integer to Roman

罗马数字

1~9: {“I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”};

10~90: {“X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”};

100~900: {“C”, “CC”, “CCC”, “CD”, “D”, “DC”, “DCC”, “DCCC”, “CM”};

1000~3000: {“M”, “MM”, “MMM”}.

实现

class Solution {
public:
    string intToRoman(int num) {
        string Roman_Dict[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "M", "MM", "MMM"};
        int Roman_Int[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900,
            1000, 2000, 3000
        };
        string result;

        int dict_len = 30;

        while(num > 0) {
            if(num >= Roman_Int[dict_len]) {
                result += Roman_Dict[dict_len];
                num -= Roman_Int[dict_len];
            }

            dict_len--;
        }

        return result;
    }
};

13. Roman to Integer

实现

法一:

class Solution {
public:
    int romanToInt(string s) {
        string Roman_Dict[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", 
                              "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX",
                              "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", 
                              "CM", "M", "MM", "MMM"};
        int Roman_Int[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50,
                          60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700,
                          800, 900, 1000, 2000, 3000};
        int result = 0;                  

        while(s.length() >= 1) {
            int tar = 0;
            int i;
            for(i = 30; i >= 0; i--) {
                tar = Roman_Dict[i].size();

                if(s.substr(0, tar) == Roman_Dict[i])
                    break;
            }

            result += Roman_Int[i];
            s = s.erase(0, tar);
        }                  

        return result;
    }
};

法二:

class Solution {
public:
    int romanToInt(string s) {
        if(s.length() == 0) return 0;
        int len = s.length(), num = 0, add = 0, prev = 0;
        for(int i = len-1; i >= 0; --i){
            if(s[i] == 'I') add = 1;
            else if(s[i] == 'V') add = 5;
            else if(s[i] == 'X') add = 10;
            else if(s[i] == 'L') add = 50;
            else if(s[i] == 'C') add = 100;
            else if(s[i] == 'D') add = 500;
            else if(s[i] == 'M') add = 1000;
            if(add >= prev) num += add;
            else num -= add;
            prev = add;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值