This article describes the Java implementation's solution to the mutual conversion of classic Roman numerals and Arabic numerals. Share it for your reference, as follows:
The ancient Roman Empire created a brilliant human civilization, but their number representation was indeed a bit cumbersome, especially when representing large numbers, which now seem to be unbearable, so it is rarely used in modern times. The reason for this is not because of the intelligence of the person who invented the representation, but because of a religion, the concept of 0 was forbidden in the numbers at that time!
The representation of Roman numerals mainly depends on the following basic symbols:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Here, we will only introduce the representation of numbers within 1000.
The number of times a single symbol is repeated indicates the number of times. Repeat up to 3 times. For example: CCC means 300, XX means 20, but 150 is not represented by LLL, this rule only applies to IXCM.
If the large unit of the adjacent level is on the right and the small unit is on the left, it means that the small unit is deducted from the large unit. For example: IX means 9, IV means 4, XL means 40. For more examples, see the table below. Have you found the rules?
I,1
II, 2
III, 3
IV, 4
V, 5
VI, 6
VII, 7
VIII, 8
IX, 9
X, 10
XI, 11
XII, 12
XIII,13
XIV,14
XV,15
XVI,16
XVII,17
XVIII, 18
XIX,19
XX,20
XXI,21
XXII,22
XXIX,29
XXX,30
XXXIV,34
XXXV,35
XXXIX,39
XL,40
L,50
LI,51
LV,55
LX,60
LXV,65
LXXX,80
XC,90
XCIII,93
XCV,95
XCVIII,98
XCIX,99
C,100
CC,200
CCC,300
CD,400
D,500
DC,600
DCC,700
DCCC,800
CM,900
CMXCIX,999
The requirements of this question are: Please write a program, the user enters several strings of Roman numerals, and the program outputs the corresponding decimal representation.
The input format is: the first line is the integer n, indicating that there are n Roman numerals (n<100). There will be a Roman numeral for each line later. The size of the Roman numerals shall not exceed 999.
The program is required to output n lines, which are the decimal data corresponding to Roman numerals.
For example, user input:
3
LXXX
XCIII
DCCII
Then the program should output:
80
93
702
import java.util.Scanner;/** * Please write a program, the user enters several Roman numeral strings, and the program outputs the corresponding decimal representation. * * The input format is: the first line is the integer n, indicating that there are n Roman numerals (n<100). There will be a Roman numeral for each line later. The size of the Roman numerals shall not exceed 999. * * The program is required to output n lines, which are the decimal data corresponding to Roman numerals. * * For example, user input: 3 LXXX XCIII DCCII * * Then the program should output: 80 93 702 * * @author Administrator * */public class RomeToArabic { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Wulin.com test result:"); Scanner mScanner = new Scanner(System.in); System.out.println(r2a(mScanner.nextLine())); System.out.println(a2r(mScanner.nextInt())); } /** * Convert Roman numerals to Arabic numerals* * @param m * @return */ public static int r2a(String m) { int graph[] = new int[400]; graph['I'] = 1; graph['V'] = 5; graph['X'] = 10; graph['L'] = 50; graph['C'] = 100; graph['D'] = 500; graph['M'] = 1000; char[] num = m.toCharArray(); int sum = graph[num[0]]; for (int i = 0; i < num.length - 1; i++) { if (graph[num[i]] >= graph[num[i + 1]]) { sum += graph[num[i + 1]]; } else { sum = sum + graph[num[i + 1]] - 2 * graph[num[i]]; } } return sum; } /** * Convert Arabic numerals to Roman numerals* * @param number * @return */ public static String a2r(int number) { String rNumber = ""; int[] aArray = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; String[] rArray = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "X", "IX", "V", "IV", "I" }; if (number < 1 || number > 3999) { rNumber = "-1"; } else { for (int i = 0; i < aArray.length; i++) { while (number >= aArray[i]) { rNumber += rArray[i]; number -= aArray[i]; } } return rNumber; }}Running results:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.