标签:@param next 罗马 func 情况 col class 输入 count
/** * @param {number} num * @return {string} */ var intToRoman = function(num) { const X = 10; const C = 100; const M = 1000; let res = ‘‘; let count = 1; let next = num; while(next>0){ if(next >= M){ count = ~~(next/M); res += ‘M‘.repeat(count); next = next % M; } if(next >= C){ count = ~~(next/C); if(count === 9){ res += ‘CM‘ }else if(count >= 5){ res += ‘D‘+‘C‘.repeat(count-5); }else if(count === 4){ res += ‘CD‘; }else{ res += ‘C‘.repeat(count); } next = next % C; } if(next >= X){ count = ~~(next/X); if(count === 9){ res += ‘XC‘ }else if(count >= 5){ res += ‘L‘+‘X‘.repeat(count-5); }else if(count === 4){ res += ‘XL‘; }else{ res += ‘X‘.repeat(count); } next = next % X; } switch(next){ case 9: res+=‘IX‘; break; case 8: res+=‘VIII‘; break; case 7: res+=‘VII‘; break; case 6: res+=‘VI‘; break; case 5: res+=‘V‘; break; case 4: res+=‘IV‘; break; case 3: res+=‘III‘; break; case 2: res+=‘II‘; break; case 1: res+=‘I‘; break; } if(next<X){ next = 0; } } return res; }; let num = 3999; console.log(num, intToRoman(num)) num = 3; console.log(num, intToRoman(num)) num = 4; console.log(num, intToRoman(num)) num = 9; console.log(num, intToRoman(num)) num = 10; console.log(num, intToRoman(num)) num = 11; console.log(num, intToRoman(num)) num = 58; console.log(num, intToRoman(num)) num = 68; console.log(num, intToRoman(num)) num = 598; console.log(num, intToRoman(num)) num = 698; console.log(num, intToRoman(num)) num = 1994; console.log(num, intToRoman(num))
标签:@param next 罗马 func 情况 col class 输入 count
原文地址:https://www.cnblogs.com/yanjianjiang/p/14767409.html