VB:如何编程:魔方方阵,
发布网友
发布时间:2024-05-10 19:19
我来回答
共2个回答
热心网友
时间:2024-06-02 03:55
Private Sub Command1_Click()
Dim n As Integer
Dim r() As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim s As String
Me.Cls
n = InputBox("输入N")
If n Mod 2 <> 1 Then
MsgBox "N必须是奇数", vbCritical
Exit Sub
End If
ReDim r(1 To n * n) As Integer
k = 1
i = 1
j = n \ 2 + 1
While k <= n * n
r((i - 1) * n + j) = k
i = i - 1
j = j + 1
If i < 1 Then i = n
If j > n Then j = 1
If r((i - 1) * n + j) <> 0 Then
i = i + 2
j = j - 1
If i > n Then i = i - n
If j < 1 Then j = j + n
End If
k = k + 1
Wend
s = String(Len(Trim(Str(n * n))), "0")
For i = 1 To n
For j = 1 To n
Print Format(r((i - 1) * n + j), s); " ";
Next j
Print
Next i
End Sub
Private Sub Form_Load()
Me.FontName = "宋体"
End Sub
热心网友
时间:2024-06-02 03:55
我旁边的最近用c语言写了。可惜不是vb啊