12. Integer to Roman

本文介绍了一种将整数转换为罗马数字的算法实现,输入范围限制在1到3999之间。通过定义不同数量级的罗马数字表示方式,该算法能够快速准确地完成转换。

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

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


### 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、付费专栏及课程。

余额充值