This is my second article about getting started.

Topic describes

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

Character values I 1 V 5 X 10 L 50 C 100 D 500 M 1000Copy the code

For example, the Roman numeral 2 is written as II, which is two ones side by side. Write XII as X + II. Write XXVII as XX + V + II.

Usually, the smaller Roman numerals are to the right of the larger numerals. But there are exceptions, for example, 4 is not written as IIII, it’s written as IV. The number 1 is to the left of the number 5 and represents the number 4 when the larger number 5 decreases by 1. Similarly, the number 9 represents IX. This particular rule applies only to the following six situations:

I can be put to the left of V (5) and X (10) to represent 4 and 9. X can be placed to the left of L (50) and C (100) to represent 40 and 90. C can be put to the left of D (500) and M (1000) to represent 400 and 900. Give you an integer and convert it to Roman numerals.

Thought analysis

From the analysis of the topic, we can get 13 kinds of corresponding relations between numbers and characters, namely 7 kinds of Roman characters and 6 kinds of special rule characters, as shown below

Character value M 1000 CM 900 D 500 CD 400 C 100 XC 90 L 50 XL 40 X 10 IX 9 V 5 IV 4 I 1Copy the code

Small numerals in Roman numerals are to the right of large numerals, so that we can be represented by the above correspondence, for example

Number: 1590 Indicates MDXC Number: 380 indicates CCCLXXXCopy the code

Code implementation

From the above analysis, we can implement the following code

/ * * *@param {number} num
 * @return {string}* /
const intToRoman = function (num) {
  const numSymbols = [
    [1000.'M'],
    [900.'CM'],
    [500.'D'],
    [400.'CD'],
    [100.'C'],
    [90.'XC'],
    [50.'L'],
    [40.'XL'],
    [10.'X'],
    [9.'IX'],
    [5.'V'],
    [4.'IV'],
    [1.'I']];/ / {1}
  let roman = ' '; / / {2}
  for (let i = 0; i < numSymbols.length; i++) {
    const [v, r] = numSymbols[i]; / / {3}
    while (num >= v) { / / {4}
      num -= v; / / {5}
      roman += r; / / {6}}}return roman; / / {7}
};
Copy the code

First of all, we’ve listed Numbers (1), the corresponding relationship between Roman characters then we define a variable used to store the generated string of Rome Roman (2), then we cycle the corresponding relation of array, to get the value of each loop structure (3), to determine whether a num is bigger than current loop to the corresponding relation (4), If it is larger than the current value, we decrement the current value (5) and concatenate the current symbol to the end of Roman (6), repeating the process (4, 5, 6). Finally return the resulting Roman (7).

conclusion

At this point we have completed the conversion from integers to Roman numerals. Now let’s summarize:

  1. Firstly, we analyze the composition rules of Roman numerals and get 13 kinds of corresponding relations
  2. Then according to the corresponding relationship and as large as possible to represent the numeric characters, and complete the coding work
  3. Finally, we realize the algorithm of integer to Roman numerals