Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -7,7 +7,7 @@ T pop(T)(ref T[] items, in size_t i) pure {
|
|||
return aux;
|
||||
}
|
||||
|
||||
string josephus(in int n, in int k) {
|
||||
string josephus(in int n, in int k) /*pure*/ {
|
||||
auto p = n.iota.array;
|
||||
int i;
|
||||
int[] seq;
|
||||
|
|
@ -18,7 +18,7 @@ string josephus(in int n, in int k) {
|
|||
|
||||
return format("Prisoner killing order:\n%(%(%d %)\n%)." ~
|
||||
"\nSurvivor: %d",
|
||||
std.range.chunks(seq[0 .. $ - 1], 20), seq[$ - 1]);
|
||||
seq[0 .. $ - 1].chunks(20), seq[$ - 1]);
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
|||
26
Task/Josephus-problem/Erlang/josephus-problem.erl
Normal file
26
Task/Josephus-problem/Erlang/josephus-problem.erl
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-module( josephus_problem ).
|
||||
|
||||
-export( [general_solution/3, task/0] ).
|
||||
|
||||
general_solution( Prisoners, Kill, Survive ) -> general_solution( Prisoners, Kill, Survive, erlang:length(Prisoners), [] ).
|
||||
|
||||
task() -> general_solution( lists:seq(0, 40), 3, 1 ).
|
||||
|
||||
|
||||
|
||||
general_solution( Prisoners, _Kill, Survive, Survive, Kills ) ->
|
||||
{Prisoners, lists:reverse(Kills)};
|
||||
general_solution( Prisoners, Kill, Survive, Prisoners_length, Kills ) ->
|
||||
{Skipped, [Killed | Rest]} = kill( Kill, Prisoners, Prisoners_length ),
|
||||
general_solution( Rest ++ Skipped, Kill, Survive, Prisoners_length - 1, [Killed | Kills] ).
|
||||
|
||||
kill( Kill, Prisoners, Prisoners_length ) when Kill < Prisoners_length ->
|
||||
lists:split( Kill - 1, Prisoners );
|
||||
kill( Kill, Prisoners, Prisoners_length ) ->
|
||||
kill_few( Kill rem Prisoners_length, Prisoners ).
|
||||
|
||||
kill_few( 0, Prisoners ) ->
|
||||
[Last | Rest] = lists:reverse( Prisoners ),
|
||||
{lists:reverse( Rest ), [Last]};
|
||||
kill_few( Kill, Prisoners ) ->
|
||||
lists:split( Kill - 1, Prisoners ).
|
||||
9
Task/Josephus-problem/Icon/josephus-problem-1.icon
Normal file
9
Task/Josephus-problem/Icon/josephus-problem-1.icon
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
procedure main(A)
|
||||
m := integer(A[1]) | 41
|
||||
c := integer(A[2]) | 3
|
||||
write("With ",m," men, counting to ",c," last position is: ", j(m,c))
|
||||
end
|
||||
|
||||
procedure j(m,c)
|
||||
return if m==1 then 0 else (j(m-1,c)+c)%m
|
||||
end
|
||||
27
Task/Josephus-problem/Icon/josephus-problem-2.icon
Normal file
27
Task/Josephus-problem/Icon/josephus-problem-2.icon
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
procedure main(args)
|
||||
n := total := integer(args[1]) | 41 # Number of people
|
||||
k := count := integer(args[2]) | 3 # Count
|
||||
s := integer(args[3])-1 | 0 # Number to save
|
||||
write("With ",n," people, counting by ",k,", the ",s+1," safe places are:")
|
||||
every write("\t",j(n,k,(n-s) to n))
|
||||
end
|
||||
|
||||
procedure j(n,k,s)
|
||||
a := k*(n-s) + 1
|
||||
q := k/(k-1.0)
|
||||
nk := n*k
|
||||
olda := a
|
||||
while a <= nk do {
|
||||
olda := a
|
||||
a := ceil(a,q)
|
||||
}
|
||||
t := nk - olda
|
||||
return t
|
||||
end
|
||||
|
||||
procedure ceil(a,q)
|
||||
n := a*q
|
||||
if n = integer(n) then return integer(n)
|
||||
n ?:= integer(tab(upto('.'))) + 1
|
||||
return n
|
||||
end
|
||||
27
Task/Josephus-problem/JavaScript/josephus-problem.js
Normal file
27
Task/Josephus-problem/JavaScript/josephus-problem.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;
|
||||
}
|
||||
}
|
||||
63
Task/Josephus-problem/PL-I/josephus-problem.pli
Normal file
63
Task/Josephus-problem/PL-I/josephus-problem.pli
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
*process or(!) source attributes xref;
|
||||
joseph: Proc Options(main);
|
||||
/* REXX **************************************************************
|
||||
* 15.11.2012 Walter Pachl - my own solution
|
||||
* 16.11.2012 Walter Pachl generalized n prisoners + w killing distance
|
||||
* and s=number of survivors
|
||||
* 03.05.2013 Walter Pachl Translated From REXX Version 1
|
||||
**********************************************************************/
|
||||
Dcl dead(0:100) Bit(1);
|
||||
Dcl (n,nn,w,s,p,found) Bin Fixed(15);
|
||||
Dcl pp Pic'99';
|
||||
Dcl killed Char(300) Var Init('killed: '); /* output of killings */
|
||||
Dcl survived Char(300) Var Init('Survivor(s): ');
|
||||
dead=''; /* nobody's dead yet */
|
||||
n=41; /* number of alive prisoners */
|
||||
nn=n; /* wrap around boundary */
|
||||
w=3; /* killing count */
|
||||
s=1; /* nuber of survivors */
|
||||
p=-1; /* start here */
|
||||
Do Until(n=s); /* until one alive prisoner */
|
||||
found=0; /* start looking */
|
||||
Do Until(found=w); /* until we have the third */
|
||||
p=p+1; /* next position */
|
||||
If p=nn Then p=0; /* wrap around */
|
||||
If ^dead(p) Then /* a prisoner who is alive */
|
||||
found=found+1; /* increment found count */
|
||||
End;
|
||||
dead(p)='1'b; /* shoot the one on this pos. */
|
||||
n=n-1;
|
||||
pp=p;
|
||||
killed=killed!!' '!!pp; /* add to output */
|
||||
End; /* End of main loop */
|
||||
Call o(killed);
|
||||
Do i=0 To nn-1; /* look for the surviving p's */
|
||||
If ^dead(i) Then Do; /* found one */
|
||||
pp=i;
|
||||
survived=survived!!' '!!pp;
|
||||
End;
|
||||
End;
|
||||
Call o(survived);
|
||||
|
||||
o: Proc(s);
|
||||
/*********************************************************************
|
||||
* Formatted Output of given string:
|
||||
* xxxxxxxxxx xxx xx xx xxx ---
|
||||
* xx xxx xxx
|
||||
* xxxxx xxx
|
||||
*********************************************************************/
|
||||
Dcl s Char(*) Var;
|
||||
Dcl p Bin Fixed(15);
|
||||
Dcl ll Bin Fixed(15) Init(72);
|
||||
Do While(length(s)>ll);
|
||||
Do p=ll+1 To 10 By -1;
|
||||
If substr(s,p,1)=' ' Then
|
||||
Leave;
|
||||
End;
|
||||
Put Edit(left(s,p))(Skip,a);
|
||||
s=repeat(' ',8)!!substr(s,p+1);
|
||||
End;
|
||||
Put Edit(s)(Skip,a);
|
||||
End;
|
||||
|
||||
End;
|
||||
Loading…
Add table
Add a link
Reference in a new issue