2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View 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
}

View 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]

View file

@ -1,41 +0,0 @@
function main($n=0,$k=0,$s=0) {
#n - number of prisoners
#k - kill every k'th prisoner
#s - number of survivors
write-host "`nn=$n k=$k s=$s" #show arguments
#Error Checking (Optional)
try {
if ([int]$n -lt 0){write-host "[n`<0] " -nonewline;$errors++}
if ([int]$k -lt 0){write-host "[k`<0] " -nonewline;$errors++}
if ([int]$s -lt 0){write-host "[s`<0] " -nonewline;$errors++}
if ([int]$s -gt [int]$n){write-host "[s`>n] " -nonewline;$errors++}
if ($errors -gt 0) {"";return}
} catch {"Oops! I found a string input.";return}
$dead = @(0) * $n+1
$nn=$n
$p=-1
while ($n -ne $s){
$found=0
while ($found -ne $k){
if ($p++ -eq $nn) { $p=0 }
if ($dead[$p] -ne 1) {$found++}
}
$dead[$p]++
$killed+="$p "
$n--
}
for($i=0;$i -le $nn-1;$i++){
if ($dead[$i] -ne 1) {$survived+="$i "}
}
write-host "Killed: $killed"
write-host "Survived: $survived"
return
}
main 5 2 1
main 41 3 1
main 41 3 3
main 2 -3 4