시배's Android
Algorithm | 백준 Kotlin 1158 요세푸스 문제 본문
1158번: 요세푸스 문제
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 5,000)
www.acmicpc.net
private fun main(){
val (n,k) = readLine()!!.split(" ").map { it.toInt() }
val people = mutableListOf<Int>().also { it.addAll(1..n) }
val result = mutableListOf<Int>()
var i = 0
while(people.size > 1){
i = (i + k-1) % people.size
result.add(people.removeAt(i))
}
result.add(people[0])
print("<${result.joinToString(", ")}>")
}
'Algorithm' 카테고리의 다른 글
Algorithm | 백준 Kotlin 6443 애너그램 (0) | 2023.09.23 |
---|---|
Algorithm | 백준 Kotlin 2553 마지막 팩토리얼 수 (0) | 2023.09.23 |
Algorithm | 백준 Kotlin 1965 상자넣기 (0) | 2023.09.16 |
Algorithm | 백준 Kotlin 15664 N과 M (10) (0) | 2023.09.16 |
Algorithm | 백준 Kotlin 2776 암기왕 (0) | 2023.09.16 |