2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: a7f4d8f2483413a6ce226cac
|
|
|
|
challengeType: 5
|
2020-09-07 16:17:22 +08:00
|
|
|
forumTopicId: 16044
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 罗马数字转换器
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:17:22 +08:00
|
|
|
<section id='description'>
|
|
|
|
把传入的数字转变为罗马数字。
|
|
|
|
转换后的<a href="http://www.mathsisfun.com/roman-numerals.html" target="_blank">罗马数字</a>字母必须都是大写。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:17:22 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(2)</code>应该返回 'II'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(2), "II");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(3)</code>应该返回 'III'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(3), "III");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(4)</code>应该返回 'IV'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(4), "IV");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(5)</code>应该返回 'V'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(5), "V");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(9)</code>应该返回 'IX'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(9), "IX");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(12)</code>应该返回 'XII'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(12), "XII");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(16)</code>应该返回 'XVI'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(16), "XVI");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(29)</code>应该返回 'XXIX'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(29), "XXIX");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(44)</code>应该返回 'XLIV'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(44), "XLIV");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(45)</code>应该返回 'XLV'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(45), "XLV");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(68)</code>应该返回 'LXVIII'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(68), "LXVIII");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(83)</code>应该返回 'LXXXIII'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(83), "LXXXIII");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(97)</code>应该返回 'XCVII'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(97), "XCVII");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(99)</code>应该返回 'XCIX'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(99), "XCIX");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(400)</code>应该返回 'CD'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(400), "CD");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(500)</code>应该返回 'D'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(500), "D");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(501)</code>应该返回 'DI'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(501), "DI");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(649)</code>应该返回 'DCXLIX'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(649), "DCXLIX");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(798)</code>应该返回 'DCCXCVIII'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(798), "DCCXCVIII");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(891)</code>应该返回 'DCCCXCI'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(891), "DCCCXCI");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(1000)</code>应该返回 'M'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(1000), "M");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(1004)</code>应该返回 'MIV'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(1004), "MIV");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(1006)</code>应该返回 'MVI'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(1006), "MVI");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(1023)</code>应该返回 'MXXIII'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(1023), "MXXIII");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(2014)</code>应该返回 'MMXIV'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(2014), "MMXIV");
|
2020-09-07 16:17:22 +08:00
|
|
|
- text: "<code>convertToRoman(3999)</code>应该返回 'MMMCMXCIX'。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(convertToRoman(3999), "MMMCMXCIX");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function convertToRoman(num) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
convertToRoman(36);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-09-07 16:17:22 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-09-07 16:17:22 +08:00
|
|
|
function convertToRoman(num) {
|
|
|
|
var ref = [['M', 1000], ['CM', 900], ['D', 500], ['CD', 400], ['C', 100], ['XC', 90], ['L', 50], ['XL', 40], ['X', 10], ['IX', 9], ['V', 5], ['IV', 4], ['I', 1]];
|
|
|
|
var res = [];
|
|
|
|
ref.forEach(function(p) {
|
|
|
|
while (num >= p[1]) {
|
|
|
|
res.push(p[0]);
|
|
|
|
num -= p[1];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res.join('');
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-09-07 16:17:22 +08:00
|
|
|
|
|
|
|
</section>
|