将1到3999之间的罗马数字转换为十进制数字

给定一个罗马数字,任务是找到对应的十进制值。

null

例子:

Input: IXOutput: 9IX is a Roman symbol which represents 9 Input: XLOutput: 40XL is a Roman symbol which represents 40Input: MCMIVOutput: 1904M is a thousand, CM is nine hundred and IV is four

罗马数字基于以下符号。

SYMBOL       VALUE  I            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

方法: 罗马数字中的数字是按降序书写的符号串(例如,M的首字母,D的后字母,等等)。但是,在一些特定情况下,为了避免四个字符连续重复(如IIII或XXXX), 减法记数法 常用的用法如下:

  • 放在 五、 十、 表示少了一个,所以是四个 四、 (一个小于5)和9是九(一个小于10)。
  • 十、 放在 L C 表示少10个,所以40个是 特大号 (10小于50)且90为 XC (十比一百少)。
  • C 放在 D M 表示少了一百,所以四百是 光盘 (一百比五百少)九百是 厘米 (比一千少一百)。

将罗马数字转换为整数的算法:

  1. 将罗马数字字符串拆分为罗马符号(字符)。
  2. 将罗马数字的每个符号转换为它所代表的值。
  3. 从索引0开始逐个获取符号:
    1. 如果符号的当前值大于或等于下一个符号的值,则将该值添加到运行总数中。
    2. 否则,将下一个符号的值与运行总数相加,以减去该值。

以下是上述算法的实现:

C++

// Program to convert Roman
// Numerals to Numbers
#include <bits/stdc++.h>
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)
{
// Value of current symbol
// is greater or equal to
// the next symbol
res = res + s1;
}
else
{
// Value of current symbol is
// less than the next symbol
res = res + s2 - s1;
i++;
}
}
else {
res = res + s1;
}
}
return res;
}
// Driver Code
int main()
{
// Considering inputs given are valid
string str = "MCMIV" ;
cout << "Integer form of Roman Numeral is "
<< romanToDecimal(str) << endl;
return 0;
}


JAVA

// 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)
{
// Value of current symbol
// is greater or equalto
// the next symbol
res = res + s1;
}
else
{
// Value of current symbol is
// less than the next symbol
res = res + s2 - s1;
i++;
}
}
else {
res = res + s1;
}
}
return res;
}
// Driver Code
public static void main(String args[])
{
RomanToNumber ob = new RomanToNumber();
// Considering inputs given are valid
String str = "MCMIV" ;
System.out.println( "Integer form of Roman Numeral"
+ " is "
+ ob.romanToDecimal(str));
}
}


python

# 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):
# Value of current symbol is greater
# or equal to the next symbol
res = res + s1
i = i + 1
else :
# Value of current symbol is greater
# or equal to the next symbol
res = res + s2 - s1
i = i + 2
else :
res = res + s1
i = i + 1
return res
# Driver code
print ( "Integer form of Roman Numeral is" ),
print (romanToDecimal( "MCMIV" ))


C#

// C# Program to convert Roman
// Numerals to Numbers
using System;
class GFG {
// This function returns value
// of a Roman symbol
public virtual 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
public virtual 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[i]);
// Getting value of symbol s[i+1]
if (i + 1 < str.Length)
{
int s2 = value(str[i + 1]);
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equalto the next symbol
res = res + s1;
}
else
{
res = res + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else {
res = res + s1;
i++;
}
}
return res;
}
// Driver Code
public static void Main( string [] args)
{
GFG ob = new GFG();
// Considering inputs given are valid
string str = "MCMIV" ;
Console.WriteLine( "Integer form of Roman Numeral"
+ " is "
+ ob.romanToDecimal(str));
}
}
// This code is contributed by Shrikant13


PHP

<?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 )
{
// Value of current symbol
// is greater or equal to
// the next symbol
$res = $res + $s1 ;
}
else
{
$res = $res + $s2 - $s1 ;
$i ++; // Value of current symbol is
// less than the next symbol
}
}
else
{
$res = $res + $s1 ;
$i ++;
}
}
return $res ;
}
// Driver Code
// Considering inputs
// given are valid
$str = "MCMIV" ;
echo "Integer form of Roman Numeral is " ,
romanToDecimal( $str ), "" ;
// This code is contributed by ajit
?>


Python3

def romanToInt(rom):
value = {
'M' : 1000 ,
'D' : 500 ,
'C' : 100 ,
'L' : 50 ,
'X' : 10 ,
'V' : 5 ,
'I' : 1
}
# Initialize previous character and answer
p = 0
ans = 0
# Traverse through all characters
n = len (rom)
for i in range (n - 1 , - 1 , - 1 ):
# If greater than or equal to previous,
# add to answer
if value[rom[i]] > = p:
ans + = value[rom[i]]
# If smaller than previous
else :
ans - = value[rom[i]]
# Update previous
p = value[rom[i]]
print (ans)
romanToInt( 'MCMIV' )


Javascript

<script>
// Program to convert Roman
// Numerals to Numberspublic
// 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;
}
// Finds decimal value of a
// given roman numeral
function romanToDecimal( str) {
// Initialize result
var res = 0;
for (i = 0; i < str.length; i++) {
// Getting value of symbol s[i]
var s1 = value(str.charAt(i));
// Getting value of symbol s[i+1]
if (i + 1 < str.length) {
var s2 = value(str.charAt(i + 1));
// Comparing both values
if (s1 >= s2) {
// Value of current symbol
// is greater or equalto
// the next symbol
res = res + s1;
} else {
// Value of current symbol is
// less than the next symbol
res = res + s2 - s1;
i++;
}
} else {
res = res + s1;
}
}
return res;
}
// Driver Code
// Considering inputs given are valid
var str = "MCMIV" ;
document.write( "Integer form of Roman Numeral"
+ " is " + romanToDecimal(str));
// This code contributed by umadevi9616
</script>


输出

Integer form of Roman Numeral is 1904

复杂性分析:

  • 时间复杂性: O(n),其中n是字符串的长度。 只需要遍历字符串一次。
  • 空间复杂性: O(1)。 因为不需要额外的空间。

另一个解决方案——

C++

// Program to convert Roman
// Numerals to Numbers
#include <bits/stdc++.h>
using namespace std;
// This function returns value
// of a Roman symbol
int romanToDecimal(string& str)
{
map< char , int > m;
m.insert({ 'I' , 1 });
m.insert({ 'V' , 5 });
m.insert({ 'X' , 10 });
m.insert({ 'L' , 50 });
m.insert({ 'C' , 100 });
m.insert({ 'D' , 500 });
m.insert({ 'M' , 1000 });
int sum = 0;
for ( int i = 0; i < str.length(); i++)
{
/*If present value is less than next value,
subtract present from next value and add the
resultant to the sum variable.*/
if (m[str[i]] < m[str[i + 1]])
{
sum+=m[str[i+1]]-m[str[i]];
i++;
continue ;
}
sum += m[str[i]];
}
return sum;
}
// Driver Code
int main()
{
// Considering inputs given are valid
string str = "MCMIV" ;
cout << "Integer form of Roman Numeral is "
<< romanToDecimal(str) << endl;
return 0;
}


JAVA

// Program to convert Roman
// Numerals to Numbers
import java.util.Map;
import java.util.HashMap;
class GFG{
private static final Map<Character,
Integer> roman = new HashMap<Character,
Integer>()
{{
put( 'I' , 1 );
put( 'V' , 5 );
put( 'X' , 10 );
put( 'L' , 50 );
put( 'C' , 100 );
put( 'D' , 500 );
put( 'M' , 1000 );
}};
// This function returns value
// of a Roman symbol
private static int romanToInt(String s)
{
int sum = 0 ;
int n = s.length();
for ( int i = 0 ; i < n; i++)
{
// If present value is less than next value,
// subtract present from next value and add the
// resultant to the sum variable.
if (i != n - 1 && roman.get(s.charAt(i)) <
roman.get(s.charAt(i + 1 )))
{
sum += roman.get(s.charAt(i + 1 )) -
roman.get(s.charAt(i));
i++;
}
else
{
sum += roman.get(s.charAt(i));
}
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
// Considering inputs given are valid
String input = "MCMIV" ;
System.out.print( "Integer form of Roman Numeral is " +
romanToInt(input));
}
}
// This code is contributed by rahuldevgarg


C#

// Program to convert Roman
// Numerals to Numbers
using System;
using System.Collections.Generic;
public class GFG {
static Dictionary< char , int > roman = new Dictionary< char , int >();
// This function returns value
// of a Roman symbol
public static int romanToInt(String s) {
int sum = 0;
int n = s.Length;
for ( int i = 0; i < n; i++) {
// If present value is less than next value,
// subtract present from next value and add the
// resultant to the sum variable.
if (i != n - 1 && roman[s[i]] < roman[s[i + 1]]) {
sum += roman[s[i + 1]] - roman[s[i]];
i++;
} else {
sum += roman[s[i]];
}
}
return sum;
}
// Driver Code
public static void Main(String[] args) {
roman[ 'I' ] = 1;
roman[ 'V' ] =5;
roman[ 'X' ] =10;
roman[ 'L' ] =50;
roman[ 'C' ] =100;
roman[ 'D' ] =500;
roman[ 'M' ] =1000;
// Considering inputs given are valid
String input = "MCMIV" ;
Console.Write( "int form of Roman Numeral is " + romanToInt(input));
}
}
// This code is contributed by Rajput-Ji


Javascript

<script>
// Program to convert Roman
// Numerals to Numbers
var roman = new Map() ;
roman.set( 'I' , 1);
roman.set( 'V' , 5);
roman.set( 'X' , 10);
roman.set( 'L' , 50);
roman.set( 'C' , 100);
roman.set( 'D' , 500);
roman.set( 'M' , 1000);
// This function returns value
// of a Roman symbol
function romanToInt( s) {
var sum = 0;
var n = s.length;
for (i = 0; i < n; i++) {
// If present value is less than next value,
// subtract present from next value and add the
// resultant to the sum variable.
if (i != n - 1 && roman.get(s.charAt(i)) < roman.get(s.charAt(i + 1))) {
sum += roman.get(s.charAt(i + 1)) - roman.get(s.charAt(i));
i++;
} else {
sum += roman.get(s.charAt(i));
}
}
return sum;
}
// Driver Code
// Considering inputs given are valid
var input = "MCMIV" ;
document.write( "Integer form of Roman Numeral is " + romanToInt(input));
// This code is contributed by Rajput-Ji
</script>


输出

Integer form of Roman Numeral is 1904

时间复杂性 –O(N) 辅助空间 –O(1)

另一个解决方案: 使用python编写更短的代码

Python3

def romanToInt(s):
translations = {
"I" : 1 ,
"V" : 5 ,
"X" : 10 ,
"L" : 50 ,
"C" : 100 ,
"D" : 500 ,
"M" : 1000
}
number = 0
s = s.replace( "IV" , "IIII" ).replace( "IX" , "VIIII" )
s = s.replace( "XL" , "XXXX" ).replace( "XC" , "LXXXX" )
s = s.replace( "CD" , "CCCC" ).replace( "CM" , "DCCCC" )
for char in s:
number + = translations[char]
print (number)
romanToInt( 'MCMIV' )


输出

1904

时间复杂性 –O(N)

辅助空间 –O(1)

参考资料: https://en.wikipedia.org/wiki/Roman_numerals 本文由 拉胡尔·阿格拉瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享