시배's Android

Algorithm | 백준 Kotlin 15664 N과 M (10) 본문

Algorithm

Algorithm | 백준 Kotlin 15664 N과 M (10)

si8ae 2023. 9. 16. 19:30
 

15664번: N과 M (10)

한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해

www.acmicpc.net

private fun main() {
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }
    val arr = readLine()!!.split(" ").map { it.toInt() }.sorted()
    fun go(i: Int, s: List<Int>) {
        if (s.size == m) {
            println(s.joinToString(" "))
            return
        }
        for (j in i until n) {
            if (j == i || arr[j] != arr[j - 1]) {
                go(j + 1, s + arr[j])
            }
        }
    }
    go(0, listOf())
}