Skip to content

Commit 564c23f

Browse files
committed
数字转化为罗马数字
1 parent fbeaa35 commit 564c23f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

algorithms/intToRoman/intToRoman.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number} num
3+
* @return {string}
4+
*/
5+
var intToRoman = function(num) {
6+
const valueSymbols = [
7+
[1000, 'M'],
8+
[900, 'CM'],
9+
[500, 'D'],
10+
[400, 'CD'],
11+
[100, 'C'],
12+
[90, 'XC'],
13+
[50, 'L'],
14+
[40, 'XL'],
15+
[10, 'X'],
16+
[9, 'IX'],
17+
[5, 'V'],
18+
[4, 'IV'],
19+
[1, 'I'],
20+
]
21+
const roman = [];
22+
for (const [value, symbol] of valueSymbols) {
23+
while (num >= value) {
24+
num -= value;
25+
roman.push(symbol);
26+
}
27+
if (num == 0) {
28+
break;
29+
}
30+
}
31+
return roman.join('');
32+
};
33+
34+
intToRoman(435);

algorithms/romanToInt/romanToInt.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var romanToInt = function(s) {
66
const map = {
77
I: 1,
8+
IV: 4,
89
V: 5,
910
IX: 9,
1011
X: 10,
@@ -31,4 +32,6 @@
3132

3233
console.log(result);
3334
return result;
34-
};
35+
};
36+
37+
romanToInt('CDXXXV')

0 commit comments

Comments
 (0)