查找峰值问题:随机生成n个正整数(假设n= 20,取值范围为0~
100),存储于数组a中,将这些数按顺序映射在坐标轴上,相邻的数用直线相连,构建了类似于“心电图”的折线:
编写VB程序统计所有的峰值(peak)、峰值数量(count)和最高点(high)。 实现上述功能的VB程序如下,请回答下列问题:
(1)
代码“List. AddItem Str( count)”中的AddItem是 (单选,填字母:A .对象名/B .属性/C .事件/D .方法);
(2)
加框处的代码有错误,请改正。
Const n = 20
Dim a(1 To n) As Integer
Private Sub Form_ Load( )
'随机生成n个数,存储于数组a中,并输出在List1中。代码略。
End Sub
Private Sub Command1_ Click( )
Dim count As Integer
Dim high As Integer
Dim peak As Integer
Dim i As Integer, j As Integer
count= 0
if a(1)> a(2) then
count = 1
high=-1
i=1
Do While i< n-1
j=i+1
If a(j)>a (j-1) Then
Do While a(j)>a(j-1)
j=j+1
Loop
count =count+1
'① List2. AddItem“第”+Str(count) +“个峰值:”+ Str( peak) If peak > high Then high = peak '② Else i =i+1 End If Loop List2. AddItem“共有:”+Str(count) +“个峰值” List2. AddItem“最高点是:”+Str( high) End if End Sub
答案: 【1】D
①peak=a(j- 1);②i=j