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

33 lines
495 B

package cap3
func Ex38(k int, nums []int) {
gcd := func(m, n int) int {
a, b := m, n
if m < n {
a, b = n, m
}
r := a % b
for r != 0 {
a = b
b = r
r = a % b
}
return b
}
numsLen := len(nums)
first, gcdNumb := 0, gcd(numsLen, k)
for i := 0; i < gcdNumb; i++ {
first = nums[i]
startIdx := i
for j := 0; j < numsLen/gcdNumb; j++ {
startIdx = (startIdx + k) % numsLen
// 交换
tmp := nums[startIdx]
nums[startIdx] = first
first = tmp
}
}
}