Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.5 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
5a23c84252665b21eecc7ec5 约瑟夫斯问题 5 josephus-problem

--description--

约瑟夫斯问题是一个严峻描述的数学难题:$ n 囚犯站在一个圆圈上,顺序编号从 0 n-1 。一名刽子手沿着圈子走,从囚犯 0 开始,移走每个 k 囚犯并杀死他。随着过程的继续,圆圈变得越来越小,直到只剩下一个囚犯,然后被释放。例如,如果 n = 5 囚犯和 k = 2 囚犯被杀的命令我们称之为“杀戮序列”将是1,3,0和4幸存者将是2。鉴于任何 nk> 0 找出哪个囚犯将成为最后的幸存者。在一个这样的事件中有41个囚犯和每3 <sup>次</sup>囚犯被杀死( K = 3 $)。其中有一个聪明的名字约瑟夫斯,他解决了这个问题,站在幸存的位置,并继续讲述这个故事。他是哪个号码?写一个函数,以囚犯的初始数量和'k'作为参数,并返回幸存的囚犯的数量。

--hints--

josephus应该是一个功能。

assert(typeof josephus == 'function');

josephus(30,3)应该返回一个数字。

assert(typeof josephus(30, 3) == 'number');

josephus(30,3)应该回29

assert.equal(josephus(30, 3), 29);

josephus(30,5)应该返回3

assert.equal(josephus(30, 5), 3);

josephus(20,2)应该返回9

assert.equal(josephus(20, 2), 9);

josephus(17,6)应该回归2

assert.equal(josephus(17, 6), 2);

josephus(29,4)应该返回2

assert.equal(josephus(29, 4), 2);

--seed--

--seed-contents--

function josephus(init, kill) {

}

--solutions--

function josephus(init, kill) {
  var Josephus = {
    init: function(n) {
      this.head = {};
      var current = this.head;
      for (var i = 0; i < n - 1; i++) {
        current.label = i + 1;
        current.next = {
          prev: current
        };
        current = current.next;
      }
      current.label = n;
      current.next = this.head;
      this.head.prev = current;
      return this;
    },
    kill: function(spacing) {
      var current = this.head;
      while (current.next !== current) {
        for (var i = 0; i < spacing - 1; i++) {
          current = current.next;
        }
        current.prev.next = current.next;
        current.next.prev = current.prev;
        current = current.next;
      }
      return current.label;
    }
  }

  return Josephus.init(init).kill(kill)
}