The title

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

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.

1. Topic analysis

  • I, V, X, L, C, D, and M.

  • [Special rules]

    1. I can be put to the left of V (5) and X (10) to represent 4 and 9.
    2. X can be placed to the left of L (50) and C (100) to represent 40 and 90.
    3. C can be put to the left of D (500) and M (1000) to represent 400 and 900.

Two, begin to solve the problem

  1. The idea is to first define the Roman numeral dictionary corresponding to each number.
  2. Then according to the rules of calculation, we can know that Roman numeral strings of stitching is from big to small for Mosaic, is from the “thousand to one” (M to I) for joining together, so that when we get to the dictionary keys, ready to begin accumulating traversal, it is necessary to reverse the keys to, from big to small order.
  3. Finally, the traversal is started, exiting the current traversal at each value.
/ * * *@param {number} num
 * @return {string}* /
var intToRoman = function(num) {
    if(! num)return "";
    // 1. First define the Roman numeral dictionary corresponding to each number.
    const NUMBER_DICT = {
        1: 'I'.4: 'IV'.5: 'V'.9: 'IX'.10: 'X'.40: 'XL'.50: 'L'.90: 'XC'.100: 'C'.400: 'CD'.500: 'D'.900: 'CM'.1000: 'M'
    };
    let lastNum = num;
    let romanStr = ' ';
    / / 2. Then according to the rules of calculation, we can know that Roman numeral strings of stitching is from big to small for Mosaic, is from the "thousand to one" (M to I) for joining together, so that when we get to the dictionary keys, ready to begin accumulating traversal, it is necessary to reverse the keys to, from big to small order.
    let numbers = Object.keys(NUMBER_DICT);
    numbers = numbers.map(i= >Number(i));
    numbers.reverse();
    while (lastNum > 0) {
        let len = 0;
        while (len < numbers.length) {
            if (lastNum >= numbers[len]) {
                // 3. Finally start traversal and exit the current traversal at each value.
                lastNum-=numbers[len];
                romanStr+=NUMBER_DICT[numbers[len]]
                break; } len++; }}return romanStr;
}
Copy the code

Well, there is a result, but it seems to be a long way to define the dictionary, then extract the keys, and then reverse it

/ * * *@param {number} num
 * @return {string}* /
var intToRoman = function(num) {
    if(! num)return "";
    // const NUMBER_DICT_ROMAN = {
    // 1: 'I',
    // 4: 'IV',
    // 5: 'V',
    // 9: 'IX',
    // 10: 'X',
    // 40: 'XL',
    // 50: 'L',
    // 90: 'XC',
    // 100: 'C',
    // 400: 'CD',
    // 500: 'D',
    // 900: 'CM',
    // 1000: 'M'
    // };
    // 1. The first step is to define the corresponding Roman numeral array.
    const NUMBER_DICT = [1000.900.500.400.100.90.50.40.10.9.5.4.1];
    const NUMBER_DICT_ROMAN = ['M'.'CM'.'D'.'CD'.'C'.'XC'.'L'.'XL'.'X'.'IX'.'V'.'IV'.'I']
    let lastNum = num;
    let romanStr = ' ';
    / / 2. Then according to the rules of calculation, we can know that Roman numeral strings of stitching is from big to small for Mosaic, is from the "thousand to one" (M to I) for joining together, so that when we get to the dictionary keys, ready to begin accumulating traversal, it is necessary to reverse the keys to, from big to small order.
    // let numbers = Object.keys(NUMBER_DICT);
    // numbers = numbers.map(i=>Number(i));
    // numbers.reverse();
    while (lastNum > 0) {
        let len = 0;
        while (len < NUMBER_DICT.length) {
            if (lastNum >= NUMBER_DICT[len]) {
                // 3. Finally start traversal and exit the current traversal at each value.
                lastNum-=NUMBER_DICT[len];
                romanStr+=NUMBER_DICT_ROMAN[len];
                // romanStr+=NUMBER_DICT[numbers[len]]
                break; } len++; }}return romanStr;
}
Copy the code

Well, that seems to be it

The last

The original address

To be continued