每一列、每一行及對角線的和都要相等
用一個textbox輸入3,然後就列出3*3的魔術方陣
2006-06-01 10:17:57 · 1 個解答 · 發問者 no nickname 3 in 電腦與網際網路 ➔ 程式設計
'我只會奇數,偶數的公式忘了,等想到再補出來XD(記得好像4,8,12...跟6,10,14..解法不一樣)'表單置1個TextBox跟1個CommandButtonPrivate Sub Command1_Click() Dim A() As Integer, N%, X%, Y%, R%, C%, I%, J% If IsNumeric(Text1) = False Then Exit Sub If Int(Text1) < 3 Then Exit Sub Cls N = Int(Text1) If N Mod 2 = 0 Then MsgBox "請輸入奇數": Exit Sub FontName = "細明體" AutoRedraw = True Caption = "每排、每列和每條對角線的和:" & (1 + N ^ 2) * N ^ 2 / 2 / N N = N - 1 ReDim A(N, N) C = N \ 2 For I = 1 To (N + 1) ^ 2 If A(R, C) Then X = (C + 1) Mod (N + 1) Y = R - 1 If Y < 0 Then Y = N If A(Y, X) Then R = R + 1 Else R = Y C = X End If End If A(R, C) = I Next C = (N + 1) ^ 2 For I = 0 To N For J = 0 To N Print String(Len(CStr(C)) - Len(CStr(A(I, J))) + 2, " ") & A(I, J); Next Print Tab(1) NextEnd Sub
2006-06-01 16:26:59 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋