程序运行时,单击“生成随机数”按钮Command1,则生成20个[1,50]区间内的整数,存储在数组a中,并显示在文本框Text1中;单击“排序并去重”按钮Command2,则将数组a中数据进行从低到高排序,并将排序结果显示在文本框Text2中,将去重后结果显示在文本框Text3中,在标签Label4中显示共删除的重复数个数,程序运行界面如下图所示。
实现上述功能的VB程序如下,请回答下列问题:
Dim a(1 To 20) As Integer
Const n = 20
Private Sub Command1_Click()
Dim i As Integer, s1 As String
Randomize
s1 = “ ”
For i = 1 To n
a(i) = Int(Rnd * 50 + 1)
s1 = s1 + Str(a(i))
Next i
Text1.Text = s1
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer, p As Integer, tt As Integer
Dim ans As Integer, s2 As String, s3 As String
For i = 1 To n - 1
p = i
For j = n To i + 1 Step -1
If a(j) < a(p) Then p = j
Next j
If a(i) < > a(p) Then
tt = a(i)
a(i) = a(p)
a(p) = tt
End If
Next i
s2 = “ ”:s3 = “ ”
ans = 0
For i = 1 To n
s2 = s2 + Str(a(i))
Next i
Text2.Text = s2
s3 = Str(a(1))
For i = 2 To n
If a(i) < > q Then
q = a(i)
Else
End If
Next i
Text3.Text = s3
Label4.Caption = Str(ans)
End Sub