Problem 63
Integer to Roman
Medium · 20 pts · String, Math
Problem
Convert an integer between 1 and 3999 to a Roman numeral. The symbols are I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000, and six subtractive forms are used instead of four repeated symbols: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900. A greedy method works: go through the 13 values 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 from largest to smallest, and while the number is at least the current value, write its symbol and subtract it. For example, 1994 = M + CM + XC + IV = MCMXCIV.
Input
A single integer N.
Output
Print N as a Roman numeral.
Constraints
1 <= N <= 3999
Example input
3
Example output
III
Explanation
For 3, the largest value that fits is I = 1. Writing I and subtracting 1 three times uses up the number, so the result is III.