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