Home java How to convert a decimal number system to the Roman?

How to convert a decimal number system to the Roman?




Please tell me where to start what would be translated from the decimal number system to the Roman and back?

This is a proper method for example 2

public static String decimal2Roman (int x) {...}
public static int roman2Decimal (String s) {...}

Answer 1, Authority 100%

You have two separate tasks. To solve both of you should read the Roman numeral system .

The first translated from the decimal system – is a challenge to the with the remainder of the division operator (%) . Algorithm of actions similar to the following:

  1. Look bit number. (Assume the number at input program 23, such Torr bit number 2)
  2. Take the minimum divisor for the number with the bit of step 1. (See table of Roman numerals for the bit 2, we have two Roman number X – 10, L – 50. Take the lesser of 10)
  3. Divide the input number divisible 23/10 = 2
  4. Write the calculation result as a Roman number in the string (2 times X, turn XX)
  5. Divide the input number with a remainder of 23% 2 = 3
  6. If bit result of division with remainder over 2 pass in paragraph 1, or paragraph 7
  7. Take the remainder of the division and find matching it in Roman notation 3 – & gt; III
    8 Add the calculation result as a Roman number end of the line (XX + III = XXIII)

The second problem is more interesting – it is the task of parsing the string. You will need to find an appropriate character from the string line in the decimal system.
Because in the Roman numeral system can not write more than 3 identical symbols in a row (IV rather than IIII, XL instead of XXXX), then you have to find a match is not one character but two.

As a result, you get an array of characters, such as roman numbers XXXIV, you should get an array [10,10,10,4]. Summarizing the elements of an array, you will find the number you want, in the example it would be 34.

To check can use calculator online.

Answer 2, Authority 25%

Most of the problems have already been solved before you. If you do not learn, it is usually not necessary to reinvent the wheel. Here are links to a test solution (I apologize for such a short answer):

  1. From Roman to Arab
  2. From Arabic to Roman

PS: the site with these links there are thousands of other solutions of relatively simple tasks for bolshistvo programming languages ​​


Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions