现有n根棍子,第i根棍子的长度为a<sub>1</sub>,现设计了以下VB程序段,想要从中选出三根棍子组成周长尽可能长的三角形,输出最大的周长;若无法组成三角形,则输出0。如当n=5,a={2,3,4,5,10}时,输出12,即选择了3、4、5。当n=4,a={4,5,10,20}时,无法组成三角形,输出0。实现上述功能的VB程序代码如下,但加框处代码有误,请改正。
Dim a(1 To 1000) As Integer
Dim n As Integer
Private Sub Form_Load()
'确定n的值和数组a的各个元素值,即每根棍子的长度值,代码略
End Sub
Function max (x As Integer, y As Integer) As Integer
If x>y Then
max=x
Else
max=y
End If
End Function
Private Sub Command1_Click()
Dim i As Integer,
J As Integer, K As Integer
Dim ans As
Integer, c As Integer, longest As Integer,
rest As
Integer
ans=0
'让i<j<k,保证棍子不会被重复选中
For i=1 To n
For j=i+1
To n
For
k=j+1 To n
c=a(i)+a(j)+a(k)
longest= '①
rest=c-longest 'rest 保存最短的两条边的和
If Then '②
ans=max(ans, c)
End If
Next k
Next j
Next i
Print ans
End Sub