logo

Konvertieren von Dezimalzahlen zwischen 1 und 3999 in römische Ziffern

Probieren Sie es bei GfG Practice aus ' title=

Wandeln Sie eine gegebene Ganzzahl in die entsprechende römische Zifferndarstellung um.

Notiz: Nachfolgend finden Sie die Liste der römischen Symbole (einschließlich der subtraktiven Fälle):



SymbolWert
ICH1
IV4
V5
IX9
X10
XL40
L50
XC90
C100
CD400
D500
CM900
M1000

Beispiele: 

Eingang: 9
Ausgabe: IX
Erläuterung: 9 wird als „IX“ in römischen Ziffern in subtraktiver Schreibweise geschrieben, wobei eine kleinere Ziffer vor einer größeren steht.

Mark Zuckerberg Ausbildung
  • I= 1X= 10
  • IXbedeutet 10 - 1 = 9

Eingang: 40
Ausgabe: XL
Erläuterung: 40 wird in römischen Ziffern in subtraktiver Schreibweise als „XL“ geschrieben, wobei eine kleinere Ziffer vor einer größeren steht.



  • X= 10 L = 50
  • XLbedeutet 50 - 10 = 40

[Allzwecklösung] – O(n) Zeit und O(n) Raum

Vergleichen Sie die angegebene Zahl mit Basiswerten in der Reihenfolge 1000 900 500 400 100 90 50 40 10 9 5 4 1. Sobald wir den größten Basiswert gefunden haben, der kleiner als die angegebene Zahl ist, dividieren wir die Zahl durch den Basiswert und wiederholen den Vorgang für kleinere Basiswerte und Quotienten. Wir addieren das römische Symbol, das dem gefundenen Basiswert entspricht, so oft zum Ergebnis, dass es dem Quotienten entspricht, und wiederholen den Vorgang für den Rest.

Lassen Sie uns den Ansatz anhand eines Beispiels 3549 verstehen

Iteration 1



Methodenteilzeichenfolge Java
  • Seit 3549 >= 1000 ; Der größte Basiswert wird zunächst 1000 sein.
  • Teilen Sie 3549/1000. Quotient = 3 res = 'MMM' (Anmerkung: M gehört zu 1000)
  • Rest = 549

Iteration 2

  • 1000 > 549 >= 500 ; Der größte Basiswert beträgt 500.
  • Teilen Sie 549/500. Wann immer = 1 .res = '300'
  • Rest = 49

Iteration 3

  • 50 > 49 >= 40 ; Der größte Basiswert ist 40.
  • Teilen Sie 49/40. Quotient = 1 res = 'MMMDXL'
  • Rest = 9.

Iteration 4

  • Nummer 9 ist in der Liste vorhanden. res = 'MMMDXL'
  • Rest = 0.
C++
#include    using namespace std; // Function to convert decimal to Roman Numerals string toRoman(int x) {  // array of values and symbols  vector<int> base = {1 4 5 9 10 40 50 90 100 400 500 900 1000};  vector<string> sym = {'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M'};  // to store result  string res = '';  // Loop from the right side to find  // the largest smaller base value  int i = base.size() - 1;  while (x > 0) {  int div = x / base[i];  while (div) {  res += sym[i];  div--;  }    // Repeat the process for remainder  x = x % base[i];    i--;  }  return res; } int main() {  int x = 3549;  cout << toRoman(x);  return 0; } 
Java
// Function to convert decimal to Roman Numerals public class RomanConverter {  public static String toRoman(int x) {    // array of values and symbols  int[] base = {1 4 5 9 10 40 50 90 100 400 500 900 1000};  String[] sym = {'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M'};  // to store result  StringBuilder res = new StringBuilder();  // Loop from the right side to find  // the largest smaller base value  int i = base.length - 1;  while (x > 0) {  int div = x / base[i];  while (div > 0) {  res.append(sym[i]);  div--;  }    // Repeat the process for remainder  x = x % base[i];  i--;  }  return res.toString();  }  public static void main(String[] args) {  int x = 3549;  System.out.println(toRoman(x));  } } 
Python
# Function to convert decimal to Roman Numerals def to_roman(x): # array of values and symbols base = [1 4 5 9 10 40 50 90 100 400 500 900 1000] sym = ['I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M'] # to store result res = '' # Loop from the right side to find # the largest smaller base value i = len(base) - 1 while x > 0: div = x // base[i] while div: res += sym[i] div -= 1 # Repeat the process for remainder x %= base[i] i -= 1 return res x = 3549 print(to_roman(x)) 
C#
// Function to convert decimal to Roman Numerals public class RomanConverter {  public static string ToRoman(int x) {    // array of values and symbols  int[] baseValues = {1 4 5 9 10 40 50 90 100 400 500 900 1000};  string[] symbols = {'I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M'};  // to store result  string res = '';  // Loop from the right side to find  // the largest smaller base value  int i = baseValues.Length - 1;  while (x > 0) {  int div = x / baseValues[i];  while (div > 0) {  res += symbols[i];  div--;  }    // Repeat the process for remainder  x %= baseValues[i];  i--;  }  return res;  }  public static void Main() {  int x = 3549;  Console.WriteLine(ToRoman(x));  } } 
JavaScript
// Function to convert decimal to Roman Numerals function toRoman(x) {    // array of values and symbols  const base = [1 4 5 9 10 40 50 90 100 400 500 900 1000];  const sym = ['I' 'IV' 'V' 'IX' 'X' 'XL' 'L' 'XC' 'C' 'CD' 'D' 'CM' 'M'];  // to store result  let res = '';  // Loop from the right side to find  // the largest smaller base value  let i = base.length - 1;  while (x > 0) {  let div = Math.floor(x / base[i]);  while (div) {  res += sym[i];  div--;  }    // Repeat the process for remainder  x %= base[i];  i--;  }  return res; } let x = 3549; console.log(toRoman(x)); 

Ausgabe
MMMDXLIX

Zeitkomplexität: O(n) wobei n die Länge der Antwortzeichenfolge ist, in der die Konvertierung gespeichert wird.
Hilfsraum: An)

Java konvertiert einen String in eine Ganzzahl

[Für begrenzte Reichweite] – O(n) Zeit und O(n) Raum

Die Idee basiert auf der Tatsache, dass wir nur über einen begrenzten Bereich zur Umwandlung von 0 bis 3999 verfügen. Wir isolieren die Ziffern, die den Tausender-, Hunderter-, Zehner- und Einerstellen entsprechen, und ordnen dann jede Ziffer basierend auf ihrem Positionswert ihrem jeweiligen römischen Ziffernäquivalent zu.

  • Speichern Sie Zuordnungen des Zeichens M für verschiedene Quotienten 0 1 2 3
  • Speichern Sie Zuordnungen von C L und I für verschiedene Quotienten von 0 bis 9.

Mithilfe der obigen Zuordnungen generieren wir direkt die Ergebniszeichenfolge.

C++
#include    using namespace std; // Function to convert decimal to Roman Numerals string toRoman(int val) {   // storing roman values of digits from 0-9  // when placed at different places  vector<string> m = {'' 'M' 'MM' 'MMM'};  vector<string> c = {'' 'C' 'CC' 'CCC' 'CD'  'D' 'DC' 'DCC' 'DCCC' 'CM'};  vector<string> x = {'' 'X' 'XX' 'XXX' 'XL'  'L' 'LX' 'LXX' 'LXXX' 'XC'};  vector<string> i = {'' 'I' 'II' 'III' 'IV'  'V' 'VI' 'VII' 'VIII' 'IX'};  // Converting to roman  string thousands = m[val / 1000];  string hundreds = c[(val % 1000) / 100];  string tens = x[(val % 100) / 10];  string ones = i[val % 10];  string ans = thousands + hundreds + tens + ones;  return ans; } int main() {  int val = 3549;  cout << toRoman(val);  return 0; } 
Java
import java.util.*; public class GfG {  // Function to convert decimal to Roman Numerals  public static String toRoman(int val) {   // storing roman values of digits from 0-9  // when placed at different places  String[] m = {'' 'M' 'MM' 'MMM'};  String[] c = {'' 'C' 'CC' 'CCC' 'CD'  'D' 'DC' 'DCC' 'DCCC' 'CM'};  String[] x = {'' 'X' 'XX' 'XXX' 'XL'  'L' 'LX' 'LXX' 'LXXX' 'XC'};  String[] i = {'' 'I' 'II' 'III' 'IV'  'V' 'VI' 'VII' 'VIII' 'IX'};  // Converting to roman  String thousands = m[val / 1000];  String hundreds = c[(val % 1000) / 100];  String tens = x[(val % 100) / 10];  String ones = i[val % 10];  String ans = thousands + hundreds + tens + ones;  return ans;  }  public static void main(String[] args) {  int val = 3549;  System.out.println(toRoman(val));  } } 
Python
# Function to convert decimal to Roman Numerals def toRoman(val): # storing roman values of digits from 0-9 # when placed at different places m = ['' 'M' 'MM' 'MMM'] c = ['' 'C' 'CC' 'CCC' 'CD' 'D' 'DC' 'DCC' 'DCCC' 'CM'] x = ['' 'X' 'XX' 'XXX' 'XL' 'L' 'LX' 'LXX' 'LXXX' 'XC'] i = ['' 'I' 'II' 'III' 'IV' 'V' 'VI' 'VII' 'VIII' 'IX'] # Converting to roman thousands = m[val // 1000] hundreds = c[(val % 1000) // 100] tens = x[(val % 100) // 10] ones = i[val % 10] ans = thousands + hundreds + tens + ones return ans if __name__ == '__main__': val = 3549 print(toRoman(val)) 
C#
using System; public class GfG {  // Function to convert decimal to Roman Numerals  public static string toRoman(int val) {   // storing roman values of digits from 0-9  // when placed at different places  string[] m = {'' 'M' 'MM' 'MMM'};  string[] c = {'' 'C' 'CC' 'CCC' 'CD'  'D' 'DC' 'DCC' 'DCCC' 'CM'};  string[] x = {'' 'X' 'XX' 'XXX' 'XL'  'L' 'LX' 'LXX' 'LXXX' 'XC'};  string[] i = {'' 'I' 'II' 'III' 'IV'  'V' 'VI' 'VII' 'VIII' 'IX'};  // Converting to roman  string thousands = m[val / 1000];  string hundreds = c[(val % 1000) / 100];  string tens = x[(val % 100) / 10];  string ones = i[val % 10];  string ans = thousands + hundreds + tens + ones;  return ans;  }  public static void Main(string[] args) {  int val = 3549;  Console.WriteLine(toRoman(val));  } } 
JavaScript
// Function to convert decimal to Roman Numerals function toRoman(val) {   // storing roman values of digits from 0-9  // when placed at different places  let m = ['' 'M' 'MM' 'MMM'];  let c = ['' 'C' 'CC' 'CCC' 'CD'  'D' 'DC' 'DCC' 'DCCC' 'CM'];  let x = ['' 'X' 'XX' 'XXX' 'XL'  'L' 'LX' 'LXX' 'LXXX' 'XC'];  let i = ['' 'I' 'II' 'III' 'IV'  'V' 'VI' 'VII' 'VIII' 'IX'];  // Converting to roman  let thousands = m[Math.floor(val / 1000)];  let hundreds = c[Math.floor((val % 1000) / 100)];  let tens = x[Math.floor((val % 100) / 10)];  let ones = i[val % 10];  let ans = thousands + hundreds + tens + ones;  return ans; } let val = 3549; console.log(toRoman(val)); 

Ausgabe
MMMDXLIX