编写“矩形面积”程序,实现如下功能:按一定比例随机生成a(1)至a(100)数组元素的值:0或1(0多1少);将这100个数组元素,按行依次转化为10×10的二维阵列; 当数组元素的值为0 时显示成“□”,当数组元素的值为1时显示成“◆”,并在列表框 List1中输出。寻找阵列中,由“□”字符构造出的最大面积的矩形,在标签Label1中显示最大面积所占的单位数。程序运行界面如图所示。
实现上述功能的VB代码如下:
Dim a(1 To 100) As Integer Private Sub Command1_Click()
Dim i As Integer, s As Integer, smax As Integer ‘用于随机生成数据,并将阵列按规则在列表框中输出,该部分功能的代码略。
smax = 0
For i = 1 To 100
If a(i) = 0 Then s = search(i) If s > smax Then smax = s
Next i
Label1.Caption = "最大面积的矩形,占" + Str(smax) + "个单位。" End Sub
Function search(i As Integer) As Integer
Dim x As Integer, x1 As Integer, xend As Integer Dim y As Integer, y1 As Integer
Dim s As Integer
x = (i - 1) Mod 10 + 1 '起始列号 y = (i - 1) \ 10 + 1 '起始行号 x1 = x
y1 = y xend = 10
search = 0
Do While y1 <= 10
If a((y1 - 1) * 10 + x) = 1 Then Exit Do '判断 x1 = x
Do While x1 <= xend
If a((y1 - 1) * 10 + x1) = 1 Then
x1 = x1 – 1 '改错
Else
x1 = x1 + 1
End If Loop
y1 = y1 + 1
s = (x1 - x) * ( ) '填空 If s > search Then search = s
Loop
End Function
请回答下列问题: