Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
https://leetcode.com/problems/integer-to-roman/
阿拉伯数字转罗马数字。
造表,把有所的基数都放到表中,主要是要放入IV,IX这类数,方便处理。
从大到小匹配表中的数,一轮循环搞定。
1 /** 2 * @param {number} num 3 * @return {string} 4 */ 5 var intToRoman = function(num) { 6 var number = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]; 7 var roman = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"]; 8 var tmp, index = 12, res = ""; 9 while(index >= 0){ 10 if(number[index] > num){ 11 index--; 12 continue; 13 } 14 while(num >= number[index]){ 15 num -= number[index]; 16 res += roman[index]; 17 } 18 index--; 19 } 20 return res; 21 };