Roman Numerals
InterviewTime limit1sMemory limit128 MB
Convert two Roman numerals to integers, sum them, and output the sum as both an Arabic and a Roman numeral.
- Level
Easy3 of 10
- Topics
- String, Math, Implementation
- Solved
- No attempts yet
Problem
Roman numerals write natural numbers with the seven symbols I, V, X, L, C, D, and M.
A valid Roman numeral follows these rules.
- In general, symbols with larger values are written to the left of symbols with smaller values. In that case, the number's value is the sum of all symbol values.
- V, L, and D may each appear at most once in one number. I, X, C, and M may be repeated consecutively at most three times.
- A smaller symbol may appear to the left of a larger symbol only in IV, IX, XL, XC, CD, and CM. Each such pair may appear at most once in one number, and two pairs with the same smaller symbol, such as IV and IX, cannot appear together.
- Every number must be written with the fewest possible symbols.
Given two numbers written as Roman numerals, output their sum as an Arabic numeral and as a Roman numeral.
Input
The first and second lines each contain one number written as a Roman numeral. Each input number is at most 2000, and their sum is less than 4000.
Output
On the first line, output the sum as an Arabic numeral. On the second line, output the same value as a Roman numeral.