[12] 整数转罗马数字

// @lc code=start
/**
 * @param {number} num
 * @return {string}
 */
var intToRoman = function (num) {
  const nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  const strs = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
  let ans = "";
  for (let i = 0; i < nums.length; i++) {
    const n = Math.floor(num / nums[i]);
    for (let j = 0; j < n; j++) {
      ans += strs[i];
    }
    num = num % nums[i];
  }
  return ans;
};

 

posted @ 2023-11-27 13:40  人恒过  阅读(16)  评论(0)    收藏  举报