Seven Roman Letters: I (1), V (5), X (10), L (50), C (100), D (500), M (1000)
Rules:
(1) Small numbers put the right side of the added, and V, L, D can only be used once;
(2) Small (only limited to I, X, C) to the left to indicate minus small, and only one;
(3) ligatures can not exceed 3 times;
(4) Underline a number, indicating that the value is 1000 times times higher.
1. Convert decimal number (0-3999) to Roman numerals
public class solution{public
static void Main (string[] args) {
System.out.println (Inttoroman (2344));
private static String inttoroman (int num) {
string[][] c = new string[][]{
{"", "I", "II", "III", "IV", "V", "VI", " VII "," VIII "," IX "},
{" "," X "," XX "," XXX "," XL "," L "," LX "," LXX "," LXXX "," XC "},
{" "," C "," CC "," CCC "," CD "," D "," DC "," DCC "," DCCC "," CM "},
{" "," M "," MM "," MMM "}
};
String Roman = "";
Roman + = "" + (c[3][num/1000%));
Roman + = "" + (c[2][num/100%));
Roman + = "" + (c[1][num/10%));
Roman + = "" + (c[0][num%));
Return Roman
}
}
2. Convert Roman numerals to decimal numbers (0-3999)
public class Solution {public
int romantoint (String s) {
map<character, integer> Map = new Hashmap<chara Cter, integer> ();
Map.put (' I ', 1);
Map.put (' V ', 5);
Map.put (' X ', ten);
Map.put (' L ', m);
Map.put (' C ', m);
Map.put (' D ',);
Map.put (' M ', 1000);
int length = S.length ();
int result = Map.get (S.charat (length-1));
for (int i = length-1 i > 0; i--) {
if (Map.get (S.charat (i)) > Map.get (S.charat (i-1))} {result-
= Map.get ( S.charat (i-1));
else{result
= Map.get (S.charat (i-1));
}
return result;
}
Notation of Roman Numerals: I,II,III represents the number of fingers (1,2,3), one hand is V (5), Two hands are X (two V, i.e. 10), 100 (a century century) is half c,c (i.e. 50), 1000 is m (Mille Latin 1000, Like mile), D is 500 separate notes.