vb 生成幻方矩阵的题目,求指点!
答案:2 悬赏:50 手机版
解决时间 2021-11-12 00:18
- 提问者网友:几叶到寒
- 2021-11-11 08:37
vb 生成幻方矩阵的题目,求指点!
最佳答案
- 五星知识达人网友:毛毛
- 2021-11-11 10:09
Option Explicit
Option Base 1
Private Sub Form_Click()
Dim t As Integer, suji As Integer, r As Integer, c As Integer
Dim i As Integer, j As Integer, i1 As Integer, j1 As Integer
Dim a() As Integer
t = InputBox("请输入一个奇数:")
If t Mod 2 = 0 Then
MsgBox "您输入的不是奇数,请重新输入!"
Exit Sub
End If
ReDim a(t, t)
suji = t ^ 2
r = 1: c = (t + 1) / 2: i = 1
For i1 = 1 To t
For j1 = 1 To t
a(r, c) = i: i = i + 1
If j1 = t Then Exit For
r = r - 1: c = c + 1
If r = 0 Then r = t
If c > t Then c = 1
Next j1
r = r + 1
Next i1
For i1 = 1 To t
For j1 = 1 To t
Print Format(a(i1, j1), "@@@");
Next j1
Print
Next i1
Print
End Sub
Option Base 1
Private Sub Form_Click()
Dim t As Integer, suji As Integer, r As Integer, c As Integer
Dim i As Integer, j As Integer, i1 As Integer, j1 As Integer
Dim a() As Integer
t = InputBox("请输入一个奇数:")
If t Mod 2 = 0 Then
MsgBox "您输入的不是奇数,请重新输入!"
Exit Sub
End If
ReDim a(t, t)
suji = t ^ 2
r = 1: c = (t + 1) / 2: i = 1
For i1 = 1 To t
For j1 = 1 To t
a(r, c) = i: i = i + 1
If j1 = t Then Exit For
r = r - 1: c = c + 1
If r = 0 Then r = t
If c > t Then c = 1
Next j1
r = r + 1
Next i1
For i1 = 1 To t
For j1 = 1 To t
Print Format(a(i1, j1), "@@@");
Next j1
Next i1
End Sub
全部回答
- 1楼网友:时间的尘埃
- 2021-11-11 11:36
'以前根据规则写的,你这代码也不全啊
Function fc(n)
Dim arr() As Long, i As Long, j As Long, m As Long, x As Long, y As Long
ReDim arr(1 To n, 1 To n)
If n < 1 Or n Mod 2 = 0 Then
MsgBox "输入一个大于0的奇数!"
Exit Function
End If
i = 1: j = Int(n / 2) + 1: arr(i, j) = 1 '规则1:第一行中间列为1
For m = 2 To n ^ 2
x = i: i = i - 1: If i = 0 Then i = n '规则2:行-1,如果行=0 则行置n
y = j: j = j + 1: If j = n + 1 Then j = 1 '规则3:列+1,如果列=n+1,则列置1
If arr(i, j) = 0 Then
arr(i, j) = m
Else '规则4:如果目标位有数值,则把数m写入当前行的下一行,列不变
i = x + 1: j = y: arr(i, j) = m
End If
Next
For i = 1 To n
For j = 1 To n
Print arr(i, j),
Next
Print
Next
End Function
Private Sub Command1_Click()
Cls
fc 3
End Sub
Function fc(n)
Dim arr() As Long, i As Long, j As Long, m As Long, x As Long, y As Long
ReDim arr(1 To n, 1 To n)
If n < 1 Or n Mod 2 = 0 Then
MsgBox "输入一个大于0的奇数!"
Exit Function
End If
i = 1: j = Int(n / 2) + 1: arr(i, j) = 1 '规则1:第一行中间列为1
For m = 2 To n ^ 2
x = i: i = i - 1: If i = 0 Then i = n '规则2:行-1,如果行=0 则行置n
y = j: j = j + 1: If j = n + 1 Then j = 1 '规则3:列+1,如果列=n+1,则列置1
If arr(i, j) = 0 Then
arr(i, j) = m
Else '规则4:如果目标位有数值,则把数m写入当前行的下一行,列不变
i = x + 1: j = y: arr(i, j) = m
End If
Next
For i = 1 To n
For j = 1 To n
Print arr(i, j),
Next
Next
End Function
Private Sub Command1_Click()
Cls
fc 3
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯