Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Josephus-problem/JavaScript/josephus-problem-1.js
Normal file
27
Task/Josephus-problem/JavaScript/josephus-problem-1.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
7
Task/Josephus-problem/JavaScript/josephus-problem-2.js
Normal file
7
Task/Josephus-problem/JavaScript/josephus-problem-2.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function Josephus(n, k, s) {
|
||||
s = s | 1
|
||||
for (var ps=[], i=n; i--; ) ps[i]=i
|
||||
for (var ks=[], i=--k; ps.length>s; i=(i+k)%ps.length) ks.push(ps.splice(i, 1))
|
||||
document.write((arguments.callee+'').split(/\s|\(/)[1], '(', [].slice.call(arguments, 0), ') -> ', ps, ' / ', ks.length<45?ks:ks.slice(0,45)+',...' , '<br>')
|
||||
return [ps, ks]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue