시배's Android
Algorithm | 백준 Kotlin 12101 1,2,3 더하기 2 본문
12101번: 1, 2, 3 더하기 2
n을 1, 2, 3의 합으로 나타내는 방법 중에서 사전 순으로 k번째에 오는 것을 출력한다. k번째 오는 식이 없는 경우에는 -1을 출력한다.
www.acmicpc.net
private fun main() {
val (N, K) = readLine()!!.split(" ").map { it.toInt() }
val res = ArrayList<String>()
fun go(now: Int, str: String) {
if (now == N) {
res.add(str)
return
}
if (now > N) {
return
}
for (i in 1..3) {
go(now + i, "$str+$i")
}
}
for (i in 1..3) {
go(i, "$i")
}
println(if (res.size < K) -1 else res[K - 1])
}
'Algorithm' 카테고리의 다른 글
Algorithm | 백준 Kotlin 7576 토마토 (1) | 2023.10.07 |
---|---|
Algorithm | 백준 Kotlin 2606 바이러스 (0) | 2023.10.07 |
Algorithm | 백준 Kotlin 5548 행성 탐사 (0) | 2023.10.07 |
Algorithm | 백준 Kotlin 3273 두 수의 합 (0) | 2023.10.07 |
Algorithm | 백준 Kotlin 6443 애너그램 (0) | 2023.09.23 |