Data una stringa in forma romana, il compito è convertire questa stringa romana in un numero intero.
numeri romani si basano sui seguenti simboli:
Simbolo | Valore |
|---|---|
IO quanto pesa kat timpf? | 1 |
IN | 5 |
X scarica youtube con vlc | 10 |
l | cinquanta |
C Java genera un numero casuale | 100 |
D | 500 |
M listnode java | 1000 |
Esempio:
Pratica consigliata dal numero romano al numero intero Provalo!Ingresso: IX
Produzione: 9
Spiegazione: IX è un simbolo romano che rappresenta 9Ingresso: XL
Produzione: 40
Spiegazione: XL è un simbolo romano che rappresenta 40Ingresso: MCMIV
Produzione: 1904
Spiegazione: M è mille, CM è novecento e IV è quattroin Giava
Approccio: Un numero in numeri romani è una stringa di questi simboli scritti in ordine discendente (ad esempio prima la M, seguita dalla D, ecc.). Tuttavia, in alcuni casi specifici, per evitare che quattro caratteri vengano ripetuti in successione (come IIII o XXXX), notazione sottrattiva è spesso usato come segue:
- IO posto prima IN O X indica uno in meno, quindi quattro lo è IV (uno meno di 5) e 9 è IX (uno meno di 10).
- X posto prima l O C indica dieci in meno, quindi quaranta lo è XL (10 meno di 50) e 90 è XC (dieci meno di cento).
- C posto prima D O M indica cento in meno, quindi quattrocento lo è CD (cento meno di cinquecento) e novecento lo è CM (cento meno di mille).
Algoritmo per convertire i numeri romani in numeri interi:
- Dividere la stringa del numero romano in simboli romani (carattere).
- Converti ogni simbolo dei numeri romani nel valore che rappresenta.
- Prendi i simboli uno per uno partendo dall'indice 0:
- Se il valore corrente del simbolo è maggiore o uguale al valore del simbolo successivo, aggiungi questo valore al totale parziale.
- altrimenti sottrai questo valore aggiungendo il valore del simbolo successivo al totale parziale.
Di seguito è riportata l'implementazione dell'algoritmo di cui sopra:
C++ // Program to convert Roman // Numerals to Numbers #include using namespace std; // This function returns value // of a Roman symbol int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } // Returns decimal value of // roman numaral int romanToDecimal(string& str) { // Initialize result int res = 0; // Traverse given input for (int i = 0; i < str.length(); i++) { // Getting value of symbol s[i] int s1 = value(str[i]); if (i + 1 < str.length()) { // Getting value of symbol s[i+1] int s2 = value(str[i + 1]); // Comparing both values if (s1>= s2) { // Il valore del simbolo corrente // è maggiore o uguale al // simbolo successivo res = res + s1; } else { // Il valore del simbolo corrente è // inferiore al simbolo successivo res = res + s2 - s1; io++; } } else { res = res + s1; } } restituisce res; } // Codice driver int main() { // Considerando gli input forniti come validi string str = 'MCMIV'; cout<< 'Integer form of Roman Numeral is ' << romanToDecimal(str) << endl; return 0; }> C // Program to convert Roman // Numerals to Numbers #include #include // This function returns value // of a Roman symbol int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } // Returns decimal value of // roman numaral int romanToDecimal(char str[]) { // Initialize result int res = 0; // Traverse given input for (int i = 0; i < strlen(str); i++) { // Getting value of symbol s[i] int s1 = value(str[i]); if (i + 1 < strlen(str)) { // Getting value of symbol s[i+1] int s2 = value(str[i + 1]); // Comparing both values if (s1>= s2) { // Il valore del simbolo corrente // è maggiore o uguale al // simbolo successivo res = res + s1; } else { // Il valore del simbolo corrente è // inferiore al simbolo successivo res = res + s2 - s1; io++; } } else { res = res + s1; } } restituisce res; } // Codice driver int main() { // Considerando che gli input forniti sono validi char str[10] = 'MCMIV'; printf('La forma intera del numero romano è %d',romanToDecimal(str)); restituire 0; }> Giava // Program to convert Roman // Numerals to Numbers import java.util.*; public class RomanToNumber { // This function returns // value of a Roman symbol int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } // Finds decimal value of a // given roman numeral int romanToDecimal(String str) { // Initialize result int res = 0; for (int i = 0; i < str.length(); i++) { // Getting value of symbol s[i] int s1 = value(str.charAt(i)); // Getting value of symbol s[i+1] if (i + 1 < str.length()) { int s2 = value(str.charAt(i + 1)); // Comparing both values if (s1>= s2) { // Il valore del simbolo corrente // è maggiore o uguale // al simbolo successivo res = res + s1; } else { // Il valore del simbolo corrente è // inferiore al simbolo successivo res = res + s2 - s1; io++; } } else { res = res + s1; } } restituisce res; } // Codice driver public static void main(String args[]) { RomanToNumber ob = new RomanToNumber(); // Considerando gli input forniti come validi String str = 'MCMIV'; System.out.println('La forma intera del numero romano' + ' è ' + ob.romanToDecimal(str)); } }> Pitone # Python program to convert Roman Numerals # to Numbers # This function returns value of each Roman symbol def value(r): if (r == 'I'): return 1 if (r == 'V'): return 5 if (r == 'X'): return 10 if (r == 'L'): return 50 if (r == 'C'): return 100 if (r == 'D'): return 500 if (r == 'M'): return 1000 return -1 def romanToDecimal(str): res = 0 i = 0 while (i < len(str)): # Getting value of symbol s[i] s1 = value(str[i]) if (i + 1 < len(str)): # Getting value of symbol s[i + 1] s2 = value(str[i + 1]) # Comparing both values if (s1>= s2): # Il valore del simbolo corrente è maggiore # o uguale al simbolo successivo res = res + s1 i = i + 1 else: # Il valore del simbolo corrente è maggiore # o uguale al simbolo successivo res = res + s2 - s1 i = i + 2 else: res = res + s1 i = i + 1 return res # Codice driver print('La forma intera del numero romano è'), print(romanToDecimal('MCMIV'))>'>C#= s2) { // Il valore del simbolo corrente è maggiore // o uguale al simbolo successivo res = res + s1; } else { res = res + s2 - s1; io++; // Il valore del simbolo corrente è // inferiore al simbolo successivo } } else { res = res + s1; io++; } } restituisce res; } // Codice driver public static void Main(string[] args) { GFG ob = new GFG(); // Considerando che gli input forniti sono validi string str = 'MCMIV'; Console.WriteLine('Forma intera del numero romano' + ' è ' + ob.romanToDecimal(str)); } } // Questo codice è un contributo di Shrikant13> Javascript >
PHP // Program to convert Roman // Numerals to Numbers // This function returns // value of a Roman symbol function value($r) { if ($r == 'I') return 1; if ($r == 'V') return 5; if ($r == 'X') return 10; if ($r == 'L') return 50; if ($r == 'C') return 100; if ($r == 'D') return 500; if ($r == 'M') return 1000; return -1; } // Returns decimal value // of roman numeral function romanToDecimal(&$str) { // Initialize result $res = 0; // Traverse given input for ($i = 0; $i < strlen($str); $i++) { // Getting value of // symbol s[i] $s1 = value($str[$i]); if ($i+1 < strlen($str)) { // Getting value of // symbol s[i+1] $s2 = value($str[$i + 1]); // Comparing both values if ($s1>= $s2) { // Il valore del simbolo corrente // è maggiore o uguale al // simbolo successivo $res = $res + $s1; } else { $res = $res + $s2 - $s1; $i++; // Il valore del simbolo corrente è // inferiore al simbolo successivo } } else { $res = $res + $s1; $i++; } } restituisce $res; } // Codice driver // Considerando gli input // forniti sono validi $str ='MCMIV'; echo 'La forma intera del numero romano è ', romanToDecimal($str), '
'; // Questo codice è fornito da ajit ?>> Produzione
Integer form of Roman Numeral is 1904>
Analisi della complessità:
- Complessità temporale: O(n), dove n è la lunghezza della stringa.
È richiesto un solo attraversamento della stringa. - Spazio ausiliario: O(1), poiché non è richiesto spazio aggiuntivo.