// 罗马数字(Roman numerals) 由如下7种记号排列而成。
// Symbol I V X L C D M
// Value 1 5 10 50 100 500 1000
// Standard form表示中,subtractive notation 将发生作用,
// 意味着4 是IV 而不是IIII,9 是IX而不是VIIII,
// 该规则适用于40(XL)和 900(CM)等等。
// 简单来说,罗马数字遵从如下规则。
// 记号从左往右,值从大到小进行排列。
// 从左往右,如果下一个记号的值更大,则代表减法,否则是加法。
// 请实现integerToRoman(),传入的整数都在有效范围内。
// integerToRoman(123)
// // 'CXXIII'
// integerToRoman(1999)
// // 'MCMXCIX'
// integerToRoman(3420)
// // 'MMMCDXX'
/**
* @param {number} integer
* @returns {string} str - roman numeral string
*/
// function integerToRoman(num) {
// // your code here
// const thousands = ['', 'M', 'MM', 'MMM'];
// const hundreds = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'];
// const tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
// const ones = ['', "I", 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
// return thousands[Math.floor(num / 1000)] +
// hundreds[Math.floor((num % 1000) / 100)] +
// tens[Math.floor((num % 100) / 10)] +
// ones[num % 10]
// };
function integerToRoman(num) {
const romanMap = {
1000: 'M',
900: "CM",
500: "D",
400: "CD",
100: "C",
90: 'XC',
50: 'L',
40: 'XL',
10: "X",
9: "IX",
5: "V",
4: "IV",
1: "I"
};
const keys = Object.keys(romanMap).map(Number).sort((a,b) => {
return b - a;
});
console.log('keys',keys);
let result = '';
for(const key of keys) {
while(num >= key) {
result += romanMap[key];
num -= key;
};
};
return result;
};
console.log(integerToRoman(1999));
console