<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="author" content="杨欣">
<title></title>
</head>
<body>
<script>
// 整数转罗马数字
function intoRoman(num) {
let keys = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
let values = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
let res = ''
for (let i = 0; i < keys.length; i++) {
while (num >= keys[i]) {
num = num - keys[i]
res += values[i]
}
}
return res
}
console.log(intoRoman(3));
// 罗马数字转整数
var romanToInt = function (s) {
var hashNum = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
var result = 0;
for (let i = 0; i < s.length; i++) {
hashNum[s[i]] < hashNum[s[i + 1]] ? result -= hashNum[s[i]] : result += hashNum[s[i]]
}
return result;
};
console.log(romanToInt('III'));
</script>
</body>
</html>