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


罗马数字是基于古罗马数字系统的一种数字表示法。字母M、D、C、L、X、V和I分别代表1000、500、100、50、10、5和1,我们将在下面的小节中讨论所有主要的符号。在这个问题中,我们得到一个罗马数字字符串,我们的任务是将1到3999范围内的罗马数字转换为十进制。

让我们看看下面的例子和解释,以便更好地理解这个问题。

输入1

str = "MCMIX"

输出1

1909

解释

M是1000的罗马表示法,

CM是900的罗马表示法,

IX是9的罗马表示法。

输入2

str = "IV"

输出

2

解释

IV是4的罗马表示法。

输入3

str = "VI"

输出3

6
符号
I 1
IV 4
IX 9
V 5
X 10
XL 40
XC 90
L 50
C 100
CD 400
CM 900
D 500
M 1000

方法

我们已经看到了上面给定罗马数字字符串的例子,让我们来看一下方法。

根据观察,罗马数字符号遵循降序排列来表示数字(例如,C先出现,然后是X等等)。然而,它在某些情况下也遵循减法表示法,以防止四个字符连续重复(例如CCCC或IIII)。

  • I出现在V或X之前表示少一。

    例如:4 = IV(比五少一),

    9 = IX(比十少一)

  • X出现在L或C之前表示少十。

    例如:40 = XL(比五十少十),

    90 = XC(比一百少十)

  • C出现在D和M之前表示少一百

    例如:400 = CD(比五百少一百)

    900 = CM(比一千少一百)

让我们看看下面的代码,以便更好地理解上述方法。

例子

下面是一个Java程序,用于将1到3999之间的罗马数字转换为十进制。

Open Compiler
import java.util.*; // Create a class for initializing the function to return the Roman symbol's value. public class Solution{ // This function is created to return a Roman symbol's value. int romanValue(char ch) { if(ch=='I') return 1; else if(ch=='V') return 5; else if(ch=='X') return 10; else if(ch=='L') return 50; else if(ch=='C') return 100; else if(ch=='D') return 500; else if(ch=='M') return 1000; return -1; } // This function is created for the conversion of Roman numerals to decimal numerals int convertRomanToDecimal(String str) { // Initialize decimal value int decVal = 0; int n = str.length(); // Getting the size of the string for (int i = 0; i < n; i++) { // check if i+1 charchter exist and getting value of roman symbol str[i] and str[i+1] if (i + 1 < n && romanValue(str.charAt(i)) < romanValue(str.charAt(i + 1))) { //subtract the current value from the next value and add the decVal variable decVal = decVal + romanValue(str.charAt(i + 1)) - romanValue(str.charAt(i)); i++; // Increment the index of the string to point to the next char } // If i+1 char not exist else { decVal = decVal + romanValue(str.charAt(i)); // add the first char value } } return decVal; // Return decimal value } public static void main(String args[]) { Solution ob = new Solution(); String str = "MCMIX"; // Given string System.out.println("Roman Numeral: " + str); // Print the decimal form and call the function of conversion System.out.println("The decimal Numeral form of the Roman Numeral" + " is " + ob.convertRomanToDecimal(str)); } }

输出

Roman Numeral: MCMIX
The decimal Numeral form of the Roman Numeral is 1909

时间和空间复杂度

上述代码的时间复杂度为O(N),因为只需要遍历一次字符串。其中N是给定罗马数字字符串的大小。由于没有使用额外的空间,所以上述代码的空间复杂度为O(1)。

另一种使用哈希表的方法

例子

Open Compiler
import java.util.Map; import java.util.HashMap; public class Solution { public static final Map<Character, Integer> romanValue = new HashMap<Character, Integer>() { { put ( 'M', 1000 ); put ( 'D', 500 ); put ( 'C', 100 ); put ( 'L', 50 ); put ( 'X', 10 ); put ( 'V', 5 ); put ( 'I', 1 ); } }; // Function is created for conversion of the Roman numeral to decimal numeral private static int convertRomanToDecimal(String str) { // Initialize decimal value int decVal = 0; for (int i = 0; i < str.length(); i++) { // store numeric value of roman symbol str[i] int first = romanValue.get(str.charAt(i)); // check if i+1 charchter exist or not if (i + 1 < str.length()) { // store numeric value of roman symbol str[i+1] int second = romanValue.get(str.charAt(i + 1)); // check which value is greater first or second if (first <= second) { // if first value <= second add first value to variable decVal decVal = decVal + first; } else { // Value of first char is less than to the the second char decVal = decVal + second - first; i++; // Increment the index of string to point to next char } } // If i+1 char not exist else { decVal = decVal + first; // add the first char value } } return decVal; // Return decimal value } public static void main(String args[]) { String str = "MMMIX"; // Given string System.out.println("Roman Numeral: " + str); // print the decimal form and call function of conversion System.out.println("Decimal Numeral form of Roman Numeral" + " is " + convertRomanToDecimal(str)); } }

输出

Roman Numeral: MMMIX
Decimal Numeral form of Roman Numeral is 3009

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

结论

在本教程中,我们实现了一个Java程序,用于将1到3999之间的罗马数字转换为十进制。我们用两种方法实现了这个程序。第一种是普通函数,第二种是哈希表函数。

更新于:2023年7月11日

421 次浏览

启动你的职业生涯

通过完成课程获得认证

开始
广告