You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lavos/cap3/ex26.go

23 lines
558 B

7 months ago
package cap3
func Ex26(n int) []int {
lights := make([]bool, n)
// 默认值false,实际上意味着第一个人已经把所有灯关了,没必要再操作一遍
// 第i个人,从第二个人开始
for i := 2; i <= n; i++ {
// 编号为i的倍数的灯状态取反
for j := 1; i*j <= n; j++ {
lights[i*j-1] = !lights[i*j-1]
}
}
7 months ago
result := make([]int, 0, n)
for light, on := range lights {
if on {
result = append(result, light+1)
}
}
7 months ago
return result
7 months ago
}