12. Integer to Roman

本文详细介绍了将阿拉伯数字转换为罗马数字的两种方法,并通过具体的C++代码实现展示了转换过程中的核心逻辑。文章首先概述了罗马数字的基本构成规则,随后分别介绍了基于递减原则和特殊组合的两种转换方法。

罗马数字需要掌握的规则:

  1. 基本数字I、X、C中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个。
  2. 不能把基本数字V、L、D中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目,只能使用一个。
  3. V和X左边的小数字只能用I,且只能有1个。
  4. L和C左边的小数字只能用X,且只能有1个。
  5. D和M左边的小数字只能用C,且只能有1个。

方法1:

class Solution {
public:
    string intToRoman(int num) {
        vector<string> Roman={"I","V","X","L","C","D","M"};
        vector<int> dig={1,5,10,50,100,500,1000};
        string tmp="";
        int n=dig.size();
        int i=n-1;
        while(num!=0&&i>=0)
        {
            if(num/dig[i]!=0)
            {
                tmp+=Roman[i];
                num-=dig[i];
            }
            else
            {
                if(dig[i]==10||dig[i]==100||dig[i]==1000)
                {
                    if(dig[i]-dig[i-2]<=num)
                    {
                        num-=(dig[i]-dig[i-2]);
                        tmp+=Roman[i-2];
                        tmp+=Roman[i];
                    }
                }
                else
                {
                    if(i>0&&dig[i]-dig[i-1]<=num)
                    {
                        num-=(dig[i]-dig[i-1]);
                        tmp+=Roman[i-1];
                        tmp+=Roman[i];
                    }
                }
                i--;
            }

        }
        return tmp;
    }
};

方法2:

经过归纳,可以总结为1,4,5,9四种情况

class Solution {
public:
    string intToRoman(int num) {
        vector<string> Roman={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        vector<int> dig={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        int n=dig.size();
        int i,j,k;
        string tmp="";
        for(i=0;i<n;i++)
        {
            for(j=0,k=num/dig[i];j<k;j++)
                tmp+=Roman[i];
            num=num%dig[i];
        }
        return tmp;
    }
};


### Convert an Integer to a Roman Numeral with Subtractive Rules To convert an integer to a Roman numeral, especially accounting for subtractive combinations like IV (4), IX (9), XL (40), and so on, you can follow a structured approach using predefined mappings of integers to Roman numerals. This method ensures that the subtractive notation is applied correctly. The core idea is to use two arrays: one for the integer values and another for their corresponding Roman numeral representations. These arrays are ordered from the largest to the smallest values, including the special subtractive cases. The algorithm iterates through these values, subtracting them from the input number while appending the corresponding Roman numeral symbols to the result string. Here’s how this can be implemented in Python: ```python def int_to_roman(num): # Define the mapping of integers to Roman numerals, including subtractive cases val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1] syms = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"] roman_numeral = "" i = 0 while num > 0: while num >= val[i]: roman_numeral += syms[i] num -= val[i] i += 1 return roman_numeral ``` This function works by repeatedly subtracting the largest possible value from the given number and appending the corresponding Roman numeral symbol(s) to the result string. For example, when converting the number 1994, the function first subtracts 1000 (M), then 900 (CM), followed by 90 (XC), and finally 4 (IV), resulting in the Roman numeral "MCMXCIV" [^2]. ### Explanation of Subtractive Notation Handling Subtractive notation is used in specific cases where a smaller numeral precedes a larger one, indicating subtraction rather than addition. These cases include: - IV for 4 (5 - 1) - IX for 9 (10 - 1) - XL for 40 (50 - 10) - XC for 90 (100 - 10) - CD for 400 (500 - 100) - CM for 900 (1000 - 100) By including these subtractive combinations in the `val` and `syms` arrays, the function ensures that the correct Roman numeral representation is generated without requiring additional logic to handle these cases separately [^3]. ### Example Usage For instance, if you call `int_to_roman(1994)`, the function will return `"MCMXCIV"`, which accurately represents the year 1994 in Roman numerals [^2]. Similarly, calling `int_to_roman(58)` will return `"LVIII"`, representing 58 as L (50), V (5), and III (3).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值