2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 59637c4d89f6786115efd814
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: Hofstadter Q序列
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 5
|
|
|
|
|
videoUrl: ''
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: hofstadter-q-sequence
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
<p> <a href='https://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Q_sequence' title='wp:Hofstadter_sequence#Hofstadter_Q_sequence'>Hofstadter Q序列</a>定义为: </p><p> $ Q(1)= Q(2)= 1,\\ Q(n)= Q \ big(nQ(n-1)\ big)+ Q \ big(nQ(n-2)),\ quad n> 2. $ </p><p>它定义为<a href='http://rosettacode.org/wiki/Fibonacci sequence' title='斐波那契序列'>Fibonacci序列</a> ,但<a href='http://rosettacode.org/wiki/Fibonacci sequence' title='斐波那契序列'>Fibonacci序列中</a>的下一个术语是前两个术语的总和,在Q序列中,前两个术语告诉您在Q序列中返回多远以找到两个数字总结以制作序列的下一个术语。 </p>任务:将Hofstadter Q Sequence方程实现为JavaScript
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`hofstadterQ`是一个函数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(typeof hofstadterQ === 'function');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`hofstadterQ()`应该返回`integer`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(Number.isInteger(hofstadterQ(1000)));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`hofstadterQ(1000)`应该返回`502`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.equal(hofstadterQ(testCase[0]), res[0]);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`hofstadterQ(1500)`应该返回`755`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.equal(hofstadterQ(testCase[1]), res[1]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`hofstadterQ(2000)`应该返回`1005`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert.equal(hofstadterQ(testCase[2]), res[2]);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`hofstadterQ(2500)`应该返回`1261`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert.equal(hofstadterQ(testCase[3]), res[3]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const testCase = [1000, 1500, 2000, 2500];
|
|
|
|
|
const res = [502, 755, 1005, 1261];
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function hofstadterQ(n) {
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function hofstadterQ (n) {
|
|
|
|
|
const memo = [1, 1, 1];
|
|
|
|
|
const Q = function (i) {
|
|
|
|
|
let result = memo[i];
|
|
|
|
|
if (typeof result !== 'number') {
|
|
|
|
|
result = Q(i - Q(i - 1)) + Q(i - Q(i - 2));
|
|
|
|
|
memo[i] = result;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
return Q(n);
|
|
|
|
|
}
|
|
|
|
|
```
|