문제
You are hosting a party and do not have room to invite all of your friends. You use the following unemotional mathematical method to determine which friends to invite.
Number your friends 1, 2, . . . , K and place them in a list in this order. Then perform m rounds. In each round, use a number to determine which friends to remove from the ordered list.
The rounds will use numbers r1, r2, . . . , rm. In round i remove all the remaining people in positions that are multiples of ri (that is, ri, 2ri, 3ri, . . .) The beginning of the list is position 1.
Output the numbers of the friends that remain after this removal process.
입력
The first line of input contains the integer K (1 ≤ K ≤ 100). The second line of input contains the integer m (1 ≤ m ≤ 10), which is the number of rounds of removal. The next m lines each contain one integer. The ith of these lines (1 ≤ i ≤ m) contains ri (2 ≤ ri ≤ 100) indicating that every person at a position which is multiple of ri should be removed.
출력
The output is the integers assigned to friends who were not removed. One integer is printed per line in increasing sorted order.
나의 접근방법
일단 문제 해석하는 것부터가 문제였다. 그래서 파파고를 이용,,, 했으나 해석한 문장도 이해하기 어려웠다ㅠㅠ
암튼 이해하기로는 m라운드에 걸쳐서 r의 배수의 위치에 있는 친구는 제거한다.
코드
/* Party Invitation */
import java.util.Scanner
fun main() = with(Scanner(System.`in`)) {
val k = nextInt() // 친구 수
var m = nextInt() // 라운드 수
// 친구를 번호 매겨서 리스트에 저장
val list = mutableListOf<Int>().apply {
repeat(k) {
this.add(it+1)
}
}
// 라운드 진행
while (m > 0) {
var i = 1
val r = nextInt()
var size = list.size
var idx = 0
while (idx < size) {
if (i % r == 0) {
// r의 배수 번째 친구이면 제거
list.removeAt(idx)
size--
idx--
}
i++
idx++
}
m--
}
for (e in list) {
println(e)
}
}
'Algorithm > 백준 알고리즘' 카테고리의 다른 글
BOJ(12605) - Kotlin (0) | 2022.06.12 |
---|---|
BOJ(17608) - Kotlin (0) | 2022.06.10 |
BOJ(2605) - Kotlin (0) | 2022.06.07 |
BOJ(2609) - Kotlin (0) | 2022.06.07 |
BOJ(1436) - Kotlin (0) | 2022.06.03 |