freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../javascript-algorithms-and-d.../roman-numeral-converter/index.md

9.9 KiB
Raw Blame History

title localeTitle
Roman Numeral Converter 罗马数字转换器

:triangular_flag_on_post:如果卡住,请记得使用**Read-Search-Ask** 。尝试配对程序:busts_in_silhouette:并编写自己的代码:pencil:

:checkered_flag:问题说明:

您将创建一个将整数转换为罗马数字的程序。

相关链接

:speech_balloon:提示1

创建两个数组,一个使用罗马数字,一个使用十进制等效的新表单将非常有用。

现在尝试解决问题

:speech_balloon:提示2

如果将数字添加到引入新字母之前的数组中例如4,9和40的值它将为您节省大量代码。

现在尝试解决问题

:speech_balloon:提示3

您不能将三个以上的连续罗马数字放在一起。

现在尝试解决问题

扰流警报!

警告牌

提前解决!

:beginner:基本代码解决方案

var convertToRoman = function(num) { 
 
  var decimalValue = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]; 
  var romanNumeral = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ]; 
 
  var romanized = ''; 
 
  for (var index = 0; index < decimalValue.length; index++) { 
    while (decimalValue[index] <= num) { 
      romanized += romanNumeral[index]; 
      num -= decimalValue[index]; 
    } 
  } 
 
  return romanized; 
 } 
 
 // test here 
 convertToRoman(36); 

:rocket: 运行代码

代码说明:

  • 我们首先创建两个具有匹配索引的默认转换的数组。这些被称为decimalValueromanNumeral 。我们还创建了一个romanized的空字符串变量,它将包含最终的罗马数字。
  • 使用for循环我们循环遍历decimalValue数组的decimalValue 。我们继续循环,直到当前index的值适合num
  • 接下来,我们添加罗马数字,并将num十进制等值。
  • 最后,我们返回romanized的值。

相关链接

:sunflower:中级代码解决方案:

function convertToRoman(num) { 
 var romans = ["I", "V", "X", "L", "C", "D", "M"], 
     ints = [], 
     romanNumber = [], 
     numeral = ""; 
  while (num) { 
    ints.push(num % 10); 
    num = Math.floor(num/10); 
  } 
  for (i=0; i<ints.length; i++){ 
      units(ints[i]); 
  } 
  function units(){ 
    numeral = romans[i*2]; 
    switch(ints[i]) { 
      case 1: 
        romanNumber.push(numeral); 
        break; 
      case 2: 
        romanNumber.push(numeral.concat(numeral)); 
        break; 
      case 3: 
        romanNumber.push(numeral.concat(numeral).concat(numeral)); 
        break; 
      case 4: 
        romanNumber.push(numeral.concat(romans[(i*2)+1])); 
        break; 
      case 5: 
        romanNumber.push(romans[(i*2)+1]); 
        break; 
      case 6: 
        romanNumber.push(romans[(i*2)+1].concat(numeral)); 
        break; 
      case 7: 
        romanNumber.push(romans[(i*2)+1].concat(numeral).concat(numeral)); 
        break; 
      case 8: 
        romanNumber.push(romans[(i*2)+1].concat(numeral).concat(numeral).concat(numeral)); 
        break; 
      case 9: 
        romanNumber.push(romans[i*2].concat(romans[(i*2)+2])); 
      } 
    } 
  return romanNumber.reverse().join("").toString(); 
 } 
 
 // test here 
 convertToRoman(97); 

:rocket: 运行代码

代码说明:

  • 创建一个罗马数字( romans )数组。
  • 使用for循环在数字中创建数字 ints )数组。
  • 循环遍历数字数组基数为10并且如您所做将罗马数字基数5索引增加2 numeral = romans[i*2]罗曼numeral = romans[i*2] )。
  • 在循环内使用Switch Case将正确的罗马数字向后推送到该数组。
  • 反转Roman Numerals数组并将其转换为字符串。

相关链接

:sunflower:中级代码解决方案:

function convertToRoman(num) { 
  var romans = 
  // 10^i 10^i*5 
    ["I", "V"], // 10^0 
    ["X", "L"], // 10^1 
    ["C", "D"], // 10^2 
    ["M"]       // 10^3 
  ], 
      digits = num.toString() 
        .split('') 
        .reverse() 
        .map(function(item, index) { 
          return parseInt(item); 
        }), 
      numeral = ""; 
 
  // Loop through each digit, starting with the ones place 
  for (var i=0; i<digits.length; i++) { 
    // Make a Roman numeral that ignores 5-multiples and shortening rules 
    numeral = romans[i][0].repeat(digits[i]) + numeral; 
    // Check for a Roman numeral 5-multiple version 
    if (romans[i][1]) { 
      numeral = numeral 
        // Change occurrences of 5 * 10^i to the corresponding 5-multiple Roman numeral 
        .replace(romans[i][0].repeat(5), romans[i][1]) 
        // Shorten occurrences of 9 * 10^i 
        .replace(romans[i][1] + romans[i][0].repeat(4), romans[i][0] + romans[i+1][0]) 
        // Shorten occurrences of 4 * 10^i 
        .replace(romans[i][0].repeat(4), romans[i][0] + romans[i][1]); 
    } 
  } 
 
  return numeral; 
 } 
 
 // test here 
 convertToRoman(36); 

:rocket: 运行代码

代码说明:

  • 使用数组( romans 创建一个矩阵其中包含给定功率为10的罗马数字如果可用则为该功率的罗马数字为10倍5。
  • 将输入数字( num )转换为反转的数字( digits )数组,以便我们可以循环显示从那些位置开始向上的数字。
  • 循环通过每个数字,从个位,以及通过将每个高功率罗马数字到开始创建一个罗马数字串numeral串等于多次digit 。这个初始字符串忽略了10次幂的罗马数字也忽略了缩短规则。
  • 如果10的相关幂具有5个多重罗马数字则用numeral代替5-in-row次数与相关的5-multiple罗马数字即VL或D并缩短9 *的出现次数10 ^ i例如VIIII至VIX和4 * 10 ^ i例如XXXX至XL。订单在这里很重要
  • 最后,返回numeral

相关链接

:clipboard:捐款说明:

  • :warning: 请勿添加与任何现有解决方案类似的解决方案。如果您认为它**相似但更好** ,那么尝试合并(或替换)现有的类似解决方案。
  • 添加解决方案的说明。
  • 将解决方案分为以下类别之一 - 基本 中级高级:traffic_light:
  • 如果您添加了任何**相关的主要内容,**请仅添加您的用户名。 :warning: 不要 删除任何现有的用户名

看到:point_right: Wiki Challenge Solution Template供参考。