35 lines
1.1 KiB
Plaintext
35 lines
1.1 KiB
Plaintext
NewList prisoners.i()
|
|
|
|
Procedure f2l(List p.i())
|
|
FirstElement(p()) : tmp.i=p()
|
|
DeleteElement(p(),1) : LastElement(p())
|
|
AddElement(p()) : p()=tmp
|
|
EndProcedure
|
|
|
|
Procedure l2f(List p.i())
|
|
LastElement(p()) : tmp.i=p()
|
|
DeleteElement(p()) : FirstElement(p())
|
|
InsertElement(p()) : p()=tmp
|
|
EndProcedure
|
|
|
|
OpenConsole()
|
|
Repeat
|
|
Print(#LF$+#LF$)
|
|
Print("Josephus problem - input prisoners : ") : n=Val(Input())
|
|
If n=0 : Break : EndIf
|
|
Print(" - input steps : ") : k=Val(Input())
|
|
Print(" - input survivors : ") : s=Val(Input()) : If s<1 : s=1 : EndIf
|
|
ClearList(prisoners()) : For i=0 To n-1 : AddElement(prisoners()) : prisoners()=i : Next
|
|
If n<100 : Print("Executed : ") : EndIf
|
|
While ListSize(prisoners())>s And n>0 And k>0 And k<n
|
|
For j=1 To k : f2l(prisoners()) : Next
|
|
l2f(prisoners()) : FirstElement(prisoners()) : If n<100 : Print(Str(prisoners())+Space(2)) : EndIf
|
|
DeleteElement(prisoners())
|
|
Wend
|
|
Print(#LF$+"Surviving: ")
|
|
ForEach prisoners()
|
|
Print(Str(prisoners())+Space(2))
|
|
Next
|
|
ForEver
|
|
End
|