FCC_Intermediate Algorithm Scripting_Roman Numeral Converter
Posted yoursatan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FCC_Intermediate Algorithm Scripting_Roman Numeral Converter相关的知识,希望对你有一定的参考价值。
1.任务及要求
Roman Numeral Converter
这是一些对你有帮助的资源:
测试数据:
convert(2)
应该返回 "II"。convert(3)
应该返回 "III"。convert(4)
应该返回 "IV"。convert(5)
应该返回 "V"。convert(9)
应该返回 "IX"。convert(12)
应该返回 "XII"。convert(16)
应该返回 "XVI"。convert(29)
应该返回 "XXIX"。convert(44)
应该返回 "XLIV"。convert(45)
应该返回 "XLV"。convert(68)
应该返回 "LXVIII"。convert(83)
应该返回 "LXXXIII"。convert(97)
应该返回 "XCVII"。convert(99)
应该返回 "XCIX"。convert(500)
应该返回 "D"。convert(501)
应该返回 "DI"。convert(649)
应该返回 "DCXLIX"。convert(798)
应该返回 "DCCXCVIII"。convert(891)
应该返回 "DCCCXCI"。convert(1000)
应该返回 "M"。convert(1004)
应该返回 "MIV"。convert(1006)
应该返回 "MVI"。convert(1023)
应该返回 "MXXIII"。convert(2014)
应该返回 "MMXIV"。convert(3999)
应该返回 "MMMCMXCIX"。
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的主要内容,如果未能解决你的问题,请参考以下文章
FCC_Intermediate Algorithm Scripting_Where art thou
FCC_Intermediate Algorithm Scripting_Search and Replace
FCC_Intermediate Algorithm Scripting_Sum All Numbers in a Range