码迷,mamicode.com
首页 > 其他好文 > 详细

FCC_Intermediate Algorithm Scripting_Roman Numeral Converter

时间:2020-03-03 14:39:14      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:dev   while   存储   net   ring   wan   编程   最简   web   

1.任务及要求

Roman Numeral Converter 


将给定的数字转换成罗马数字。

所有返回的 罗马数字 都应该是大写形式。

如果你被难住了,记得使用 Read-Search-Ask尝试与他人结伴编程、编写你自己的代码。

 

2.我的解法

function convert(num) {
  
  //按个、十、百、千(1~4999)存储对应数字所对照的罗马数字
  
  var UNITS = [‘‘,‘I‘,‘II‘,‘III‘,‘IV‘,‘V‘,‘VI‘,‘VII‘,‘VIII‘,‘IX‘];
  var TENS = [‘‘,‘X‘,‘XX‘,‘XXX‘,‘XL‘,‘L‘,‘LX‘,‘LXX‘,‘LXXX‘,‘XC‘];
  var HUNDERDS = [‘‘,‘C‘,‘CC‘,‘CCC‘,‘CD‘,‘D‘,‘DC‘,‘DCC‘,‘DCCC‘,‘CM‘];
  var THOUSANDS = [‘‘,‘M‘,‘MM‘,‘MMM‘,‘MMMM‘];
  var MIN = 1;
  var MAX = 4999;
  
  var unit = ‘‘,ten=‘‘,hunderd = ‘‘,thousand = ‘‘;
  var newNum = num.toString().split(‘‘).reverse();
  var length = newNum.length;
  if(num >0 && num < 5000){
    switch(length) {
   case 1:
     unit = UNITS[newNum[0]];
     break;
   case 2:
     unit = UNITS[newNum[0]];
     ten = TENS[newNum[1]];
     break;
   case 3:
     unit = UNITS[newNum[0]];
     ten = TENS[newNum[1]];
     hunderd = HUNDERDS[newNum[2]];
     break;
   case 4:
     unit = UNITS[newNum[0]];
     ten = TENS[newNum[1]];
     hunderd = HUNDERDS[newNum[2]];
     thousand = THOUSANDS[newNum[3]];
     break;
 }
  } else {
    alert("请输入1~4999之间的数字。");
  }
 
 
  num = thousand + hunderd + ten + unit;
  
  return num;
}

convert(36);

3.发现的其他解法

//CSDN:https://blog.csdn.net/wangmc0827/article/details/72550839

function convert(num) {
  var sig = 0,ten = 0,hundred = 0,thousand = 0;
  var str = ‘‘;
  sig = num % 10;
  if(num >= 10)  ten = Math.floor(num / 10 % 10);
  if(num >= 100)  hundred = Math.floor(num / 100 % 10);
  if(num >= 1000)  thousand = Math.floor(num / 1000);
  var count = {
     thousand : thousand, hundred : hundred, ten: ten,sig : sig
  };console.log(count);
  for(var i in count){
       switch(i){
        case ‘thousand‘: 
           str += jisuan(‘M‘,count[i]);
          break;
        case ‘hundred‘: 
           str += jisuan(‘C‘,count[i]);
          break;
        case ‘ten‘: 
           str += jisuan(‘X‘,count[i]);
          break;
        case ‘sig‘: 
           str += jisuan(‘I‘,count[i]);
          break;
      }
  }
  
 return str;
}
 
function jisuan(str,num){
  if(num === 0) return ‘‘;
  if(str == ‘M‘){
    switch(num){      
      case 1: return ‘M‘;
      case 2: return ‘MM‘;
      case 3: return ‘MMM‘;
      default:
        return ‘‘;
    }
  }
  if(str == ‘C‘){
    switch(num){      
      case 1: return ‘C‘;
      case 2: return ‘CC‘;
      case 3: return ‘CCC‘;
      case 4: return ‘CD‘;
      case 5: return ‘D‘;
      case 6: return ‘DC‘;
      case 7: return ‘DCC‘;
      case 8: return ‘DCCC‘;
      case 9: return ‘CM‘;
      default:
        return ‘‘;
    }
  }
  if(str == ‘X‘){
    switch(num){     
      case 1: return ‘X‘;
      case 2: return ‘XX‘;
      case 3: return ‘XXX‘;
      case 4: return ‘XL‘;
      case 5: return ‘L‘;
      case 6: return ‘LX‘;
      case 7: return ‘LXX‘;
      case 8: return ‘LXXX‘;
      case 9: return ‘XC‘;
      default:
        return ‘‘;
    }
  }
  if(str == ‘I‘){
    switch(num){     
      case 1: return ‘I‘;
      case 2: return ‘II‘;
      case 3: return ‘III‘;
      case 4: return ‘IV‘;
      case 5: return ‘V‘;
      case 6: return ‘VI‘;
      case 7: return ‘VII‘;
      case 8: return ‘VIII‘;
      case 9: return ‘IX‘;
      default:
        return ‘‘;
    }
  }
}
 
convert(100);

// 另一个解法,好像是简书

从最大的数字开始遍历,如果num大于当前数,则减去当前数,继续进入循环。字符串则加上对应的罗马字符。


function convert(num) {
    var numArr = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
    var strArr = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
    var result = "";
    while (num > 0) {
        var i = numArr.length;
        while (i >= 0) {
            if (num >= numArr[i]) {
                result += strArr[i];
                num -= numArr[i];
            } else {
                i--;
            }
        }
    }
    return result;
}
convert(100);

这个解法是目前发现最简单的

 

FCC_Intermediate Algorithm Scripting_Roman Numeral Converter

标签:dev   while   存储   net   ring   wan   编程   最简   web   

原文地址:https://www.cnblogs.com/yoursatan/p/12402046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!