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

44 lines
935 B

5 months ago
package cap3
// Ex20 趣味矩阵
func Ex20(n int) [][]int {
matrix := make([][]int, n)
for i := 0; i < len(matrix); i++ {
matrix[i] = make([]int, n)
}
5 months ago
for i := range n {
for j := range n {
// 对角线
if i == j || i+j == n-1 {
matrix[i][j] = 0
continue
}
5 months ago
// 左上部分
if i+j < n-1 {
if i < j {
// 上
matrix[i][j] = 1
} else {
// 左
matrix[i][j] = 2
}
}
5 months ago
// 右下边部分
if i+j > n-1 {
if i > j {
// 下
matrix[i][j] = 3
} else {
// 右
matrix[i][j] = 4
}
}
}
}
5 months ago
return matrix
5 months ago
}