Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Josephus-problem/PowerShell/josephus-problem-1.psh
Normal file
20
Task/Josephus-problem/PowerShell/josephus-problem-1.psh
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
}
|
||||
12
Task/Josephus-problem/PowerShell/josephus-problem-2.psh
Normal file
12
Task/Josephus-problem/PowerShell/josephus-problem-2.psh
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# Get the prisoner order for a circle of 41 prisoners, selecting every third
|
||||
$Prisoners = Get-JosephusPrisoners -N 41 -K 3
|
||||
|
||||
# Display the prisoner order
|
||||
$Prisoners -join " "
|
||||
|
||||
# Display the last remaining prisoner
|
||||
"Last prisoner remmaining: " + $Prisoners[-1]
|
||||
|
||||
# Display the last three remaining prisoners
|
||||
$S = 3
|
||||
"Last $S remaining: " + $Prisoners[-$S..-1]
|
||||
Loading…
Add table
Add a link
Reference in a new issue