#K70002. Roman Numerals to Integer Conversion
Roman Numerals to Integer Conversion
Roman Numerals to Integer Conversion
Problem Statement:
Given a Roman numeral as an input string, convert it to its integer representation. Roman numerals are represented by the following symbols:
(I=1,\ V=5,\ X=10,\ L=50,\ C=100,\ D=500,\ M=1000).
The numeral is processed from right to left. For each symbol, if its value is less than the value of the previous processed symbol, subtract it; otherwise, add it. This rule is formally summarized as:
[ total = \sum_{i=1}^{n} (-1)^{f(i)} v(s_i) ]
where (v(s_i)) is the value of the Roman numeral at position (i) and (f(i) = 1) if (v(s_i) < v(s_{i+1})), else 0.
For example:
- (III = 1 + 1 + 1 = 3)
- (IV = 5 - 1 = 4)
- (IX = 10 - 1 = 9)
- (MCMXCIV = 1000 + (1000-100) + (100-10) + (5-1) = 1994)
Your task is to implement a function that performs this conversion.
inputFormat
Input Format:
A single line containing a non-empty string representing a valid Roman numeral. The numeral consists only of the characters 'I', 'V', 'X', 'L', 'C', 'D', and 'M'.
outputFormat
Output Format:
Output the integer value corresponding to the input Roman numeral.## sample
I
1