12. Integer to Roman

MediumMath

Seven different symbols represent Roman numerals with the following values: - I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules: - If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral. - If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Given an integer, convert it to a Roman numeral.

Examples

Input: 1994

Output: MCMXCIV

Explanation: 1000=M, 900=CM, 90=XC, 4=IV.

Constraints

  • 1 <= num <= 3999
Loading...

Run checks all cases above. Submit evaluates all test cases.