Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Combinations/Kotlin/combinations-1.kotlin
Normal file
25
Task/Combinations/Kotlin/combinations-1.kotlin
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
class Combinations(val m: Int, val n: Int) {
|
||||
private val combination = IntArray(m)
|
||||
|
||||
init {
|
||||
generate(0)
|
||||
}
|
||||
|
||||
private fun generate(k: Int) {
|
||||
if (k >= m) {
|
||||
for (i in 0 until m) print("${combination[i]} ")
|
||||
println()
|
||||
}
|
||||
else {
|
||||
for (j in 0 until n)
|
||||
if (k == 0 || j > combination[k - 1]) {
|
||||
combination[k] = j
|
||||
generate(k + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
Combinations(3, 5)
|
||||
}
|
||||
28
Task/Combinations/Kotlin/combinations-2.kotlin
Normal file
28
Task/Combinations/Kotlin/combinations-2.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import java.util.LinkedList
|
||||
|
||||
inline fun <reified T> combinations(arr: Array<T>, m: Int) = sequence {
|
||||
val n = arr.size
|
||||
val result = Array(m) { arr[0] }
|
||||
val stack = LinkedList<Int>()
|
||||
stack.push(0)
|
||||
while (stack.isNotEmpty()) {
|
||||
var resIndex = stack.size - 1;
|
||||
var arrIndex = stack.pop()
|
||||
|
||||
while (arrIndex < n) {
|
||||
result[resIndex++] = arr[arrIndex++]
|
||||
stack.push(arrIndex)
|
||||
|
||||
if (resIndex == m) {
|
||||
yield(result.toList())
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val n = 5
|
||||
val m = 3
|
||||
combinations((1..n).toList().toTypedArray(), m).forEach { println(it.joinToString(separator = " ")) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue