题解 | 约瑟夫环
约瑟夫环
https://www.nowcoder.com/practice/e417cfe32c74416ca38247f619ddb322
import java.util.Scanner
fun main(args:Array<String>){
val read = Scanner(System.`in`)
val inputLine = read.nextLine()
val tokens = inputLine.split(" ")
val a = tokens[0].toInt()
val b = tokens[1].toInt()
val c = tokens[2].toInt()
//创建人员编号列表
val people = (0..a-1).toMutableList()
var tarIndex = (b+c-1)%a //索引从1-based转化成0-based
while (people.size>1){
//移除索引位置的人
people.removeAt(tarIndex)
//计算下一个要移除的位置
tarIndex = (tarIndex+c-1) % people.size
}
var result = people[0]
println(result)
}
