HomeVolumesContestsSectionsForums PrintHelpAbout

Contests > EPOKA Programming Competition - CEN > problem:


Q2

EPOKA Programming Competition - CEN

Start: May.18.2026 at 01:00:00 PM
Finish: May.18.2026 at 03:30:00 PM
The contest is finished!
• Contest scoreboard

Contest problems

• Q1
• Q2
• Q4
• Q3
• Q5

Feedback

If you notice incorrect translations in Contester, please let author know.

Time limit 2000/4000/4000/4000 ms. Memory limit 65000/65000/65000/65000 Kb.

Roman to Integer

Roman to Integer

Question: Write a program that reads a Roman numeral string and converts it to its equivalent integer value. Roman numerals are represented by seven symbols: I, V, X, L, C, D, and M. When a smaller symbol appears immediately before a larger one, it is subtracted rather than added.

Input specification
A single line containing one valid Roman numeral string using uppercase letters only (I, V, X, L, C, D, M). The string represents a number in the range 1 to 3999.

Output specification
Print a single integer — the decimal value of the Roman numeral — followed by a newline.

Symbol reference

Symbol Value
I1
V5
X10
L50
C100
D500
M1000

Valid subtractive pairs: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.

Sample Input 1   
III
Sample Output 1   
3
Sample Input 2   
IX
Sample Output 2   
9
Sample Input 3   
MCMXCIV
Sample Output 3   
1994
Sample Input 4   
MMMDCCXLIX
Sample Output 4   
3749

Explanation (Sample 3):
MCMXCIV → M=1000, CM=900, XC=90, IV=4 → 1000 + 900 + 90 + 4 = 1994.
Hint: scan left to right; if the current symbol's value is less than the next symbol's value, subtract it instead of adding it.


Äëÿ îòïðàâêè ðåøåíèé íåîáõîäèìî âûïîëíèòü âõîä.

www.contester.ru