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/ex35.go

25 lines
572 B

7 months ago
package cap3
func Ex35(boxNum int, expectWeigh, actualWeigh int) []int {
result := make([]int, 0)
// 实际值重量正常重量差 / 10
w := (expectWeigh - actualWeigh) / 10
// w转化为2的幂的和
for w != 0 {
// k是箱子编号,从0开始, t是2的幂
k, t := 0, 1
// 找到最接近w的2的k次方t,当t大于w时结束小循环
for w-t >= 0 {
t *= 2
// t是2的k次方
k++
}
result = append(result, k-1)
w -= t / 2
}
return result
}