Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
41
Task/Josephus-problem/PowerShell/josephus-problem.psh
Normal file
41
Task/Josephus-problem/PowerShell/josephus-problem.psh
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue