logo

Konwersja liczb rzymskich na liczby całkowite

Mając dany ciąg znaków w postaci rzymskiej, zadaniem jest przekształcenie podanego ciągu rzymskiego na liczbę całkowitą.

Cyfry rzymskie opierają się na następujących symbolach:



Symbol

Wartość

I



Multiplekser 8 do 1

1

W

5



X

pokaż użytkownikom mysql

10

L

proszę

pięćdziesiąt

C

100

D

500

M

1000

Przykład:

Wejście: IX
Wyjście: 9
Wyjaśnienie: IX to rzymski symbol reprezentujący 9

pętla for w c

Wejście: XL
Wyjście: 40
Wyjaśnienie: XL to rzymski symbol reprezentujący liczbę 40

tygrys w porównaniu do lwa

Wejście: MCMIV
Wyjście: 1904
Wyjaśnienie: M to tysiąc, CM to dziewięćset, a IV to cztery

Zalecane postępowanie Liczba rzymska na liczbę całkowitą Wypróbuj!

Zbliżać się: Liczba zapisana cyframi rzymskimi to ciąg tych symboli zapisany w kolejności malejącej (np. pierwsza litera M, następnie D itp.). Jednakże w kilku określonych przypadkach, aby uniknąć powtarzania czterech znaków po sobie (takich jak IIII lub XXXX), notacja odejmująca jest często używany w następujący sposób:

  • I umieszczone wcześniej W Lub X wskazuje o jeden mniej, więc cztery to IV (jeden mniej niż 5), a 9 to IX (jeden mniej niż 10).
  • X umieszczone wcześniej L Lub C wskazuje dziesięć mniej, więc czterdzieści to XL (10 mniej niż 50) i 90 to XC (dziesięć mniej niż sto).
  • C umieszczone wcześniej D Lub M wskazuje o sto mniej, więc czterysta to płyta CD (sto mniej niż pięćset) i dziewięćset CM (sto mniej niż tysiąc).

Algorytm konwersji cyfr rzymskich na liczbę całkowitą:

  • Podziel ciąg cyfr rzymskich na symbole rzymskie (znaki).
  • Konwertuj każdy symbol cyfr rzymskich na wartość, którą reprezentuje.
  • Weź symbol jeden po drugim, zaczynając od indeksu 0:
    • Jeśli bieżąca wartość symbolu jest większa lub równa wartości następnego symbolu, to dodaj tę wartość do sumy bieżącej.
    • w przeciwnym razie odejmij tę wartość, dodając wartość następnego symbolu do sumy bieżącej.

Poniżej znajduje się implementacja powyższego algorytmu:

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) { // Wartość bieżącego symbolu // jest większa lub równa // następnemu symbolowi res = res + s1;  } else { // Wartość bieżącego symbolu jest // mniejsza niż następny symbol res = res + s2 - s1;  ja++;  } } else { res = res + s1;  } } zwrot res; } // Kod sterownika int main() { // Biorąc pod uwagę, że podane dane wejściowe są prawidłowe 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) { // Wartość bieżącego symbolu // jest większa lub równa // następnemu symbolowi res = res + s1;  } else { // Wartość bieżącego symbolu jest // mniejsza niż następny symbol res = res + s2 - s1;  ja++;  } } else { res = res + s1;  } } zwrot res; } // Kod sterownika int main() { // Uznanie, że podane dane wejściowe są prawidłowe. char str[10] = 'MCMIV';  printf('Całkowita forma cyfry rzymskiej to %d',romanToDecimal(str));  zwróć 0; }>
Jawa
// 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) { // Wartość bieżącego symbolu // jest większa lub równa // następnemu symbolowi res = res + s1;  } else { // Wartość bieżącego symbolu jest // mniejsza niż następny symbol res = res + s2 - s1;  ja++;  } } else { res = res + s1;  } } zwrot res;  } // Kod sterownika public static void main(String args[]) { RomanToNumber ob = new RomanToNumber();  // Uznanie, że podane dane wejściowe są prawidłowe. String str = 'MCMIV';  System.out.println('Całkowita forma cyfry rzymskiej' + ' to ' + ob.romanToDecimal(str));  } }>
Pyton
# 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): # Wartość bieżącego symbolu jest większa # lub równa następnemu symbolowi res = res + s1 i = i + 1 else: # Wartość bieżącego symbolu jest większa # lub równa następnemu symbolowi res = res + s2 - s1 i = i + 2 else: res = res + s1 i = i + 1 return res # Kod sterownika print('Całkowita forma cyfry rzymskiej to'), print(romanToDecimal('MCMIV'))>'>C#= s2) { // Wartość bieżącego symbolu jest większa // lub równa następnemu symbolowi res = res + s1;  } else { res = res + s2 - s1;  ja++; // Wartość bieżącego symbolu jest // mniejsza niż następny symbol } } else { res = res + s1;  ja++;  } } zwrot res;  } // Kod sterownika public static void Main(string[] args) { GFG ob = nowy GFG();  // Biorąc pod uwagę, że dane wejściowe są prawidłowe string str = 'MCMIV';  Console.WriteLine('Całkowita forma cyfry rzymskiej' + ' to ' + ob.romanToDecimal(str));  } } // Ten kod pochodzi od 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) { // Wartość bieżącego symbolu // jest większa lub równa // następnemu symbolowi $res = $res + $s1; } else { $res = $res + $s2 - $s1; $i++; // Wartość bieżącego symbolu jest // mniejsza niż wartość następnego symbolu } } else { $res = $res + $s1; $i++; } } zwróć $res; } // Kod sterownika // Uwzględnienie // podanych danych wejściowych $str ='MCMIV'; echo 'Całkowita forma cyfry rzymskiej to ', romanToDecimal($str), '
'; // Ten kod został stworzony przez ajit ?>>

Wyjście
Integer form of Roman Numeral is 1904>

Analiza złożoności:

  • Złożoność czasowa: O(n), gdzie n jest długością łańcucha.
    Wymagane jest tylko jedno przejście ciągu.
  • Przestrzeń pomocnicza: O(1), Ponieważ nie jest wymagana dodatkowa przestrzeń.