数组平分,把一个数组拆分成多个数组

package main

import "fmt"

func main() {
	arr1:=[]int{}
	//数组0~24
	for i := 0; i < 25; i++ {
		arr1=append(arr1, i)
	}
	//拆分4份
	fmt.Println("println:",splitArray(arr1,4))
	//拆分7份
	fmt.Println("println:",splitArray(arr1,7))
	//拆分12份
	fmt.Println("println:",splitArray(arr1,12))
}
//数组平分
func splitArray(arr []int, num int64) ([][]int) {
	max := int64(len(arr))
	if max < num {
		return nil
	}
	var segmens =make([][]int,0)
	quantity:=max/num
	end:=int64(0)
	for i := int64(1); i <= num; i++ {
		qu:=i*quantity
		if i != num {
			segmens= append(segmens,arr[i-1+end:qu])
		}else {
			segmens= append(segmens,arr[i-1+end:])
		}
		end=qu-i
	}
	return segmens
}

输出结果:

println1: [[0 1 2 3 4 5] [6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23 24]]
println2: [[0 1 2] [3 4 5] [6 7 8] [9 10 11] [12 13 14] [15 16 17] [18 19 20 21 22 23 24]]
println3: [[0 1] [2 3] [4 5] [6 7] [8 9] [10 11] [12 13] [14 15] [16 17] [18 19] [20 21] [22 23 24]]