# Roman Numerals and decimal conversions (Java version)

Source: Internet
Author: User

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.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.