ГлавнаяСборникиТурнирыРазделыФорумы ПечатьПомощьО системе

Разделы > Разное > задача:


Q2

Задачи раздела

• Q0
• Q1
• Q2
• Q4
• Q5

Обратная связь

Если у вас есть предложения или пожелания по работе Contester, посетите форум сайта www.contester.ru.

Лимит времени 2000/4000/4000/4000 мс. Лимит памяти 65000/65000/65000/65000 Кб.

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