输出魔方阵,魔方阵是由自然数1-n²(n位奇数)构成的方阵,其各行各列以及对角线元素之和均相等
望高手直接给出源代码
输出魔方阵,魔方阵是由自然数1-n²(n位奇数)构成的方阵,其各行各列以及对角线元素之和均相等
望高手直接给出源代码
用VB在标准EXE下给你做好了, ,点一次按钮显示一个魔方矩阵
代码如下
Dim n As Long
Function MagicMatrix() As String
Dim S() As Long
Dim x As Long, y As Long
Dim i As Long
ReDim S(1 To n, 1 To n) As Long
x = (n + 1) / 2
y = 1
S(x, y) = 1
For i = 2 To n ^ 2
If x = n And y = 1 Then
y = y + 1
ElseIf x = n Then
x = 1
y = y - 1
ElseIf y = 1 Then
x = x + 1
y = n
ElseIf S(x + 1, y - 1) <> 0 Then
y = y + 1
Else
x = x + 1
y = y - 1
End If
S(x, y) = i
Next
For i = 1 To n
For j = 1 To n
MagicMatrix = MagicMatrix & S(j, i) & " "
Next
MagicMatrix = MagicMatrix & vbCrLf
Next
End Function
Private Sub Command1_Click()
Dim p As Integer
p = 1
Do While (p = 1)
n = InputBox("Please input 奇数n")
If ((n <> 0) And (n <= 15) And (n Mod 2 <> 0)) Then
p = 0
Else
MsgBox "输入有误,请重新输入"
End If
Loop
Text1.Text = MagicMatrix
End Sub
如果你想要生成的exe可执行文件,上QQ我传给你吧,我的QQ:466698654