Mittel 0 gelöst 0% Erfolgsquote

Integer to Roman

Write a whole number from 1 to 3999 as a Roman numeral.

Das lernst du

  • Use a value table sorted from largest to smallest
  • Apply a greedy choice repeatedly
  • Handle subtractive pairs as table entries

Problem

Übersetzung anzeigen

Roman numerals use I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Symbols are written from largest to smallest and added up, except for six subtractive pairs: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900. Convert n to its standard form, which always uses a subtractive pair instead of four repeated symbols (4 is IV, never IIII).

Implement integer_to_roman(n).

Input

n: The number to convert.

Output

Return the Roman numeral as a string of uppercase letters.

Einschränkungen

1 ≤ n ≤ 3999

Beispiele

Beispiel 1
Eingabe:
n = 1994

Erwartete Ausgabe:
expected = "MCMXCIV"
Eingabe:
long long n = 1994;

Erwartete Ausgabe:
std::string expected = "MCMXCIV";
Eingabe:
long n = 1994;

Erwartete Ausgabe:
string expected = "MCMXCIV";

Erläuterung

1000 + 900 + 90 + 4 gives M, CM, XC, IV: "MCMXCIV".

Beispiel 2
Eingabe:
n = 58

Erwartete Ausgabe:
expected = "LVIII"
Eingabe:
long long n = 58;

Erwartete Ausgabe:
std::string expected = "LVIII";
Eingabe:
long n = 58;

Erwartete Ausgabe:
string expected = "LVIII";

Erläuterung

50 + 5 + 3 gives L, V, III: "LVIII".

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

List the symbols and the six subtractive pairs together, sorted by value.

Hinweis 2

Repeatedly take the largest value that still fits into what remains.

Hinweis 3

Try 4, 9 and 3999 by hand to check your table.

Erwartete Komplexität

O(1) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.