题型:综合题 题类:常考题 难易度:困难
浙江省高中信息技术 矩阵问题练习
小明依据上述描述设计了如下VB程序,在Text1中输入矩阵的行数n (n≤10),单击“生成”按钮Command1,在List1中显示n×n的螺旋矩阵。请回答下列问题:
Function pout(x As Integer) As String ‘函数功能用于数字的对齐输出
Dim i As Integer, j As Integer, y As String
y= Str(x) : j = Len(y)
For i = 1 To 5 - j
y = y +“”
Next i
End Function
Private Sub Command1_Click()
Dim a(1 To 100) As Integer
Dim i As Integer, j As Integer, m As Integer, n As Integer
Dim ts As String, t As Integer, v As Integer
n = Val (Text1.Text) :v = 1 : t = 1 ‘t用于每次填数方向上的位置修正
For m = n To 1 Step -2
For i = 1 To m ‘向右方向填数
a(t + i - 1)=v
Next i
t = t + n - 1
For i = 1 To m-1 ‘向下方向填数
a(m + t) = v
v=v+1
Next i
t = t - n
For i = m - 1 To 1 Step -1 ‘向左方向填数
a(t + i) = v
v=v+1
Next i
For i = m - 2 To 1 Step -1 ‘向上方向填数
a(t - n + 1) = v
v=v+1
t = t-n
Next i
t=t+2
Next m
For i = 1 To n ‘以图a方式在List1中输出数组数据
ts =“”
For j =To i * n
ts = ts + pout(a(j))
Next j
List1.AddItem ts
Next i
End Sub
试题篮