Skip to content

Commit 0ea56a2

Browse files
committed
Create 12.整数转罗马数字.js
1 parent a60d8cc commit 0ea56a2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

12.整数转罗马数字.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} num
3+
* @return {string}
4+
*/
5+
var intToRoman = function(num) {
6+
const table = {
7+
1: ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
8+
10: ['X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
9+
100: ['C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'],
10+
1000: ['M', 'MM', 'MMM']
11+
}
12+
13+
let result = [];
14+
let cur = 1000;
15+
while (num) {
16+
if (num / cur >= 1) {
17+
result.push(table[cur][Math.floor(num / cur) - 1]);
18+
num = num % cur;
19+
}
20+
21+
cur /= 10;
22+
}
23+
24+
return result.join('');
25+
};

0 commit comments

Comments
 (0)