奇数阶
魔方阵(
幻方阵)的生成,方法较多,Yidie教授于2007-6-15在算法设计栏目中,发表的“[原创]
奇数幻方阵生成打印程序”,是属于右下方斜行法。我也转载过一个右上方斜行法的程序。这里将刚刚学会的控件数组的添加,用在奇数魔方阵生成程序上,加上个Timer,让它动态显示:如何右上方斜行布阵的。右上方斜行法布阵规则,参见算法设计中我转载的程序。敬请大侠高手给出改进意见!程序代码如如下: Dim n As Integer, i As Integer, j As Integer, k As Integer Private Sub Command2_Click() Dim m As Integer For m = 0 To n * n - 1 Label2(m).Visible = False Next m k = 0: i = 0: j = n \ 2 Label2(k).Top = i * (11000 / n) Label2(k).Left = j * (12000 / n) Label2(k).Visible = True Timer1.Enabled = True End Sub Private Sub Form_Load() List1.Selected(0) = True Command2.Enabled = False '布阵命令失效 End Sub Private Sub list1_click() '列表框中选择魔方阵的阶数,布阵命令失效 Command2.Enabled = False End Sub Private Sub command1_click() '“生成”命令过程代码 For i = 1 To n * n - 1 Unload Label2(i) Next i n = List1.Text For i = 1 To n * n - 1 Load Label2(i) Next i For i = 0 To n * n - 1 With Label2(i) .Top = (i \ n) * (11000 / n) .Left = (i Mod n) * (12000 / n) + 100 .Width = 11000 / n .Height = 10000 / n .Caption = i + 1 .BackColor = RGB(255, 0, 0) .FontSize = 360 / n .Visible = True End With Next i Command2.Enabled = True End Sub Private Sub timer1_timer() k = k + 1 If k <n * n Then If k Mod n = 0 Then i = i + 1 Else i = IIf(i = 0, n - 1, i - 1) j = IIf(j = n - 1, 0, j + 1) End If Label2(k).Top = i * (11000 / n) Label2(k).Left = j * (12000 / n) + 100 Label2(k).Visible = True Else Timer1.Enabled = fase End If End Sub Private Sub command3_click() Unload Me End Sub
求采纳
给你个奇数阶幻方矩阵的代码:
Private Sub Form_Click()
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
Cls
Me.FontSize = 12
Print " 输出幻方矩阵 n="n
Print
For i = 0 To n - 1
For c = 0 To n - 1
Print Tab(c * 6)M(i, c)
Next c
Print
Next i
End Sub
评论列表(0条)