按照反向斜对角线(右上->左下)的方式打印M行N列的数组arrMatrix[M][N]

  • Post author:
  • Post category:其他


package main

import "fmt"

const M  = 3
const N  = 3

func print(x [M][N]int)  {
	for i:=N-1; i>=0; i-- {
		j:=0
		k:=i
		for j<M && k<N {
			fmt.Print(x[j][k]," ")
			j++
			k++
		}
		fmt.Println()
	}
	for i:=1; i<M; i++ {
		j:=i
		k:=0
		for j<M && k<N {
			fmt.Print(x[j][k]," ")
			j++
			k++
		}
		fmt.Println()
	}
}

func main()  {
	x:=[M][N]int{
		[N]int{1,2,3},
		[N]int{4,5,6},
		[N]int{7,8,9},
	}
	print(x)
}



版权声明:本文为baidu_25845567原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。