EPOKA Programming Competition - BINF |
Старт: 18.мая.2026 в 13:00:00
Финиш: 18.мая.2026 в 15:00:00
Турнир завершён!
• Турнирная таблица
|
|
Лимит времени 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 |
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
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.
Для отправки решений необходимо выполнить вход.
|