2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 59713bd26bdeb8a594fb9413
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 计算硬币
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: count-the-coins
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
<p> <a href='https://en.wikipedia.org/wiki/United_States' title='链接:https://en.wikipedia.org/wiki/United_States'>美国</a>货币有四种常见硬币: </p>季度(25美分)硬币(10美分)镍(5美分)和便士(1美分) <p>有六种方法可以换15美分: </p>一角钱和一角钱一角钱和5便士3镍2镍和5便士一镍和10便士15便士任务: <p>实现一个功能,以确定使用这些普通硬币改变一美元的方式有多少? (1美元= 100美分)。 </p>参考: <a href='http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_Temp_52' title='链接:http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_Temp_52'>麻省理工学院出版社的算法</a> 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`countCoins`是一个函数。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(typeof countCoins === 'function');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`countCoints()`应该返回242。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert.equal(countCoins(), 242);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function countCoins() {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
function countCoins() {
|
|
|
|
|
let t = 100;
|
|
|
|
|
const operands = [1, 5, 10, 25];
|
|
|
|
|
const targetsLength = t + 1;
|
|
|
|
|
const operandsLength = operands.length;
|
|
|
|
|
t = [1];
|
|
|
|
|
|
|
|
|
|
for (let a = 0; a < operandsLength; a++) {
|
|
|
|
|
for (let b = 1; b < targetsLength; b++) {
|
|
|
|
|
// initialise undefined target
|
|
|
|
|
t[b] = t[b] ? t[b] : 0;
|
|
|
|
|
|
|
|
|
|
// accumulate target + operand ways
|
|
|
|
|
t[b] += (b < operands[a]) ? 0 : t[b - operands[a]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t[targetsLength - 1];
|
|
|
|
|
}
|
|
|
|
|
```
|