Difficulty:
Given a Roman numeral less than
4,000
, convert it to a decimal number. This Wikipedia article describes Roman numerals in details, but here is a brief description:Roman numerals consist of the following symbols:
I
is 1
V
is 5
X
is 10
L
is 50
C
is 100
D
is 500
M
is 1,000A numeral is formed by a sequence of these symbols, where each symbol can be used up to 3 times to contribute to the total. Symbols are ordered left-to-right from larger to smaller, except for the following cases:
I
placed beforeV
orX
indicates one less, so 4 isIV
(one less than 5) and 9 isIX
(one less than 10)
X
placed beforeL
orC
indicates ten less, so 40 isXL
(ten less than 50) and 90 isXC
(ten less than 100)
C
placed beforeD
orM
indicates a hundred less, so 400 isCD
(a hundred less than 500) and 900 isCM
(a hundred less than 1,000)For example, 11 is
XI
, 98 isXCVIII
, 99 isXCIX
and 1944 isMCMXLIV
.
romanToDecimal("MMXVI");
2016