结果始终是奇数,要弄成偶数,咋办呢?
奇数幻方
Sub test()
Dim n As Integer, num As Integer, row, col As Integer, i As Integer, c As Integer
Dim M() As Integer
n = InputBox("请输入一个奇数:")
If n Mod 2 = 0 Then
MsgBox "请输入一个奇数!"
Exit Sub
End If
ReDim M(n - 1, n - 1)
num = n ^ 2
row = 0
col = (n - 1) \ 2
i = 1
Do While i <= num
If row = -1 Then row = n - 1
If col = n Then col = 0
M(row, col) = i
If i Mod n = 0 Then
row = row + 1
Else
row = row - 1
col = col + 1
End If
i = i + 1
Loop
[a1].Value = "输出幻方矩阵 n=" & n
For i = 0 To n - 1
For c = 0 To n - 1
Cells(i + 2, c + 1) = M(i, c)
Next c
Next i
End Sub