GO语言最右边斜打印二维数组

来源:互联网 发布:亿玛创新网络 编辑:程序博客网 时间:2024/05/18 03:57

例如如下的二维数组

var arr = [][]int{    {1, 2, 3, 4, 5, 6},    {7, 8, 9, 10, 11, 12},    {13, 14, 15, 16, 17, 18},    {19, 20, 21, 22, 23, 24},}

依次打印1,2,3,4,5,6,11,16,21,20,19,13,7,8,9,10,15,14


func printArr(arr [][]int, brow int, bcol int, row int, col int) {if len(arr) == 0 || row <= 0 || col <= 0 {return}var rowIndex = browvar colIndex = bcolfor colIndex < col {fmt.Println(arr[rowIndex][colIndex])path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))colIndex++}colIndex = colIndex - 1for rowIndex < row-1 {rowIndex++colIndex--fmt.Println(arr[rowIndex][colIndex])path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))}for colIndex > bcol {colIndex--fmt.Println(arr[rowIndex][colIndex])path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))}for rowIndex > brow+1 {rowIndex--fmt.Println(arr[rowIndex][colIndex])path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))}col = col - 2colIndex = colIndex + 1row = row - 1printArr(arr, rowIndex, colIndex, row, col)}