Const m = 6, n = 8, wt = 200
Dim d(1 To m + n) As String, a(1 To m + n) As Integer
Private Sub Form_Load()
'读取m + n 个人的体重及去向数据存入d 数组,代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, p As Integer, q As Integer, s As String
Dim num As Integer, num1 As Integer, num2 As Integer
'以下代码实现从d 数组中提取每个人的体重数据,根据去向分段存入a 数组
p = 1 : q =
For i = 1 To m + n
s = Mid(d(i), 1, Len(d(i)) - 1)
If Mid(d(i), , 1) = "W" Then
a(p) = Val(s): p = p + 1
Else
a(q) = Val(s): q = q + 1
End If
Next i
num1 = GetNum(1, m)
num2 = GetNum(m + 1, m + n)
If num1 > num2 Then num = num1 Else num = num2
Label1.Caption = "过河需要最少往返次数:" + Str(num)
End Sub
Function GetNum(head As Integer, tail As Integer) As Integer
Dim i As Integer, j As Integer, k As Integer, cnt As Integer, t As Integer
'以下代码实现对数组a 降序排序
i = head
Do While i < tail
k = i: i = tail
For j =
If a(j - 1) < a(j) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
i = j
End If
Next j
Loop
cnt = 0: i = head: j = tail
Do While i <= j
If Then j = j - 1
cnt = cnt + 1: i = i + 1
Loop
GetNum = cnt
End Function