13. Roman to Integer

本文介绍了一种将罗马数字转换为整数的方法,详细解释了罗马数字的书写规则及特殊情况处理,如IV表示4,IX表示9等。通过具体实例展示了如何解析罗马数字并计算其对应的十进制数值。

原题:
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

翻译:
罗马数字由七个不同的符号表示: I, V, X, L, C, D and M.

标识       	  数
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

举个例子, 2在罗马数字里面表示为II. 12表示为XII, 就是 X + II. 27写作XXVII, 就是 XX + V + II.

罗马数字通常采用从左到右从最大到最小的写法. 然而4不是由IIII表示. 而是用IV表示. 因为I在V的前面 V-I得到4.同样的理论可以用于9, 写作IX. 以下是六个使用减法的实例:

I 可以在 V (5) 和X (10)前面表示 4 和 9.
X 可以在 (50) 和 C (100) 前面表示 40 和 90.
C 可以在 D (500) 和 M (1000) 前面表示 和 900.
给出一个罗马数字转化为对应的十进制数. 输入保证在1到3999之间

例 1:

Input: "III"
Output: 3

例 2:

Input: "IV"
Output: 4

例 3:

Input: "IX"
Output: 9

例 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

例 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

C++程序

class Solution {
public:
    char r[7]={'I','V','X','L','C','D','M'};
    int n[7]={1,5,10,50,100,500,1000}; 
    int romanToInt(string s) {
        int num=0;
       
        for(int j=0;j<s.length();j++)
        {
            int lastnum=0;
            int i=0;
            
            for(i=0;i<8;i++)
            {
                if(s[j]==r[i])break;
            }
            
            if(j>0)
                for(lastnum=0;lastnum<8;lastnum++)
                {
                    if(s[j-1]==r[lastnum])break;
                }
            if((i>0) && (n[lastnum]<n[i])&& (j>0))
            {             
                num=num-n[lastnum]*2; 
            }
              
            
            num=num+n[i];
        }
        return num;
    }
};

在这里插入图片描述

### 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).
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值