September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
33
Task/Josephus-problem/Kotlin/josephus-problem.kotlin
Normal file
33
Task/Josephus-problem/Kotlin/josephus-problem.kotlin
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// version 1.1.3
|
||||
|
||||
fun josephus(n: Int, k: Int, m: Int): Pair<List<Int>, List<Int>> {
|
||||
require(k > 0 && m > 0 && n > k && n > m)
|
||||
val killed = mutableListOf<Int>()
|
||||
val survived = MutableList(n) { it }
|
||||
var start = k - 1
|
||||
outer@ while (true) {
|
||||
val end = survived.size - 1
|
||||
var i = start
|
||||
var deleted = 0
|
||||
while (i <= end) {
|
||||
killed.add(survived.removeAt(i - deleted))
|
||||
if (survived.size == m) break@outer
|
||||
deleted++
|
||||
i += k
|
||||
}
|
||||
start = i - end - 1
|
||||
}
|
||||
return Pair(survived, killed)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val triples = listOf(Triple(5, 2, 1), Triple(41, 3, 1), Triple(41, 3, 3))
|
||||
for (triple in triples) {
|
||||
val(n, k, m) = triple
|
||||
println("Prisoners = $n, Step = $m, Survivors = $m")
|
||||
val (survived, killed) = josephus(n, k, m)
|
||||
println("Survived : $survived")
|
||||
println("Kill order : $killed")
|
||||
println()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue