Loading...
Loading...
Old clock towers and book chapters use Roman numerals. The symbols are I=1, V=5, X=10, L=50, C=100, D=500 and M=1000. Usually symbols go from large to small and are added. When a smaller symbol comes just before a larger one, it is subtracted instead: IV is 4 and XC is 90. Read a valid Roman numeral and print its value.
Example 1
Input
XIV
Output
14
X + (V - I) = 10 + 4.
Example 2
Input
MCMXCIV
Output
1994
M + CM + XC + IV = 1000 + 900 + 90 + 4.
Try the problem first. Open one hint at a time.
Write a small method that returns the value of one symbol, using a switch.
Go left to right. If a symbol is smaller than the one after it, subtract it; otherwise add it.
The last symbol is always added.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor