2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
21
Task/Josephus-problem/PowerShell/josephus-problem-1.psh
Normal file
21
Task/Josephus-problem/PowerShell/josephus-problem-1.psh
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<lang PowerShell>
|
||||
function Get-JosephusPrisoners ( [int]$N, [int]$K )
|
||||
{
|
||||
# Just for convenience
|
||||
$End = $N - 1
|
||||
|
||||
# Create circle of prisoners
|
||||
$Prisoners = New-Object System.Collections.ArrayList ( , (0..$End) )
|
||||
|
||||
# For each starting point of the reducing circle...
|
||||
ForEach ( $Start in 0..($End - 1) )
|
||||
{
|
||||
# We subtract one from K for the one we advanced by incrementing $Start
|
||||
# Then take K modulus the length of the remaining circle
|
||||
$RoundK = ( $K - 1 ) % ( $End - $Start + 1 )
|
||||
|
||||
# Rotate the remaining prisoners K places around the remaining circle
|
||||
$Prisoners.SetRange( $Start, $Prisoners[ $Start..$End ][ ( $RoundK + $Start - $End - 1 )..( $RoundK - 1 ) ] )
|
||||
}
|
||||
return $Prisoners
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue