Convertint el nombre decimal que es troba entre 1 i 3999 a números romans
Donada un nombre enter el converteix en la seva representació numèrica romana equivalent.
NOTA: A continuació es mostra la llista de símbols romans (inclosos els casos subtractius):
| Símbol | Valorar |
|---|---|
| Jo | 1 |
| IV | 4 |
| V | 5 |
| Ix | 9 |
| X | 10 |
| XL | 40 |
| L | 50 |
| Xc | 90 |
| C | 100 |
| CD | 400 |
| D | 500 |
| Cm | 900 |
| M | 1000 |
Exemples:
Entrada: 9
Sortida: Ix
Explicació: 9 està escrit com a "IX" en números romans mitjançant notació subtractiva: situant un número més petit abans que un de més gran.
I= 1X= 10IXsignifica 10 - 1 = 9Entrada: 40
Sortida: XL
Explicació: 40 està escrit com a "XL" en números romans mitjançant notació subtractiva: situant un número més petit abans que un de més gran.
X= 10 L = 50XLsignifica 50 - 10 = 40
[Solució de propòsit general] - O (n) Temps i O (N) Espai
Compareu el nombre donat amb els valors de base de la comanda 1000 900 500 400 100 90 50 40 10 9 5 4 1. Un cop trobem el valor base més gran que és inferior al nombre donat, dividim el nombre amb el valor base i repetim el procés per a valors de base més petits i quocients. Afegim el símbol romà corresponent al valor base que es troba al nombre de vegades igual al quocient i repetir el procés per a la resta.
Comprenem l’enfocament amb un exemple 3549
Iteració 1
- Des del 3549> = 1000; El valor base més gran serà de 1000 inicialment.
- Divideix 3549/1000. Quotient = 3 res = 'mmm' (nota m pertany a 1000)
- Resta = 549
Iteració 2
- 1000> 549> = 500; El valor base més gran serà de 500.
- Divideix 549/500. Sempre que = 1 .res = '300'
- Resta = 49
Iteració 3
- 50> 49> = 40; El valor base més gran és de 40.
- Divideix 49/40. Quotient = 1 res = 'mmmdxl'
- Resta = 9.
Iteració 4
- El número 9 està present a la llista. res = 'mmmdxl'
- Resta = 0.
#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 ));
Producció
MMMDXLIX
Complexitat del temps: O (n) on n és la longitud de la cadena de resposta que emmagatzema la conversió.
Espai auxiliar: O (n)
[Per a rang limitat] - o (n) temps i espai O (n)
C++La idea es basa en el fet que tenim un rang limitat per encobrir de 0 a 3999. Aïllem els dígits corresponents als milers de desenes i llocs i, a continuació, mapant cada dígit al seu equivalent numèric romà respectiu en funció del seu valor posicional.
- Emmagatzemar mapatges de caràcter m per a diferents quotients 0 1 2 3
- Emmagatzemar mapatges de C L i I per a diferents quotients formen 0 a 9.
Utilitzant els mapatges anteriors, generem directament la cadena de resultats.
#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 ));
Producció
MMMDXLIX