解题思路:随机生成 10 条船的载重量[1,10]和费用[1,30],然后将它们按费用从小到大排序,若费用相同,则载重大的在前,运输时,先把费用低的船装满,再按费用向下将船装好,运输。
图 a
Dim f(1 To 10) As Integer, w(1 To 10) As Integer, b(1 To 10) As Integer
Private Sub Command2_Click()
Dim n As Integer, count As Integer, yf As Integer
Dim s As String, i as integer
n = Val(Text1.Text)
count = 0
yf = 0
cz = n
i = 1
Do While n > 0
If n >= w(b(i)) Then
Else
yf = yf + n * f(b(i))
End If
count=count+1
s = s + Str(b(i)) + "号船"
i=i+1
Loop
Label2.Caption = "总计费用为:" + Str(yf) + "元 " + Str(count) + " 条船,分
别为 " + s
End Sub
Private Sub form_load()
Dim t As Integer, i as integer, j as integer
List1.Clear
List1.AddItem "编号 载重 费用'"
For i = 1 To 10
w(i) = Int(Rnd * 10 + 1)
f(i) = Int(Rnd * 30 + 1)
b(i) = i
List1.AddItem Str(b(i)) + " " + Str(w(i)) + " " + Str(f(i))
Next i
For i = 1 To 9
For j = 10 To i + 1 Step -1
If Then
t = b(j): b(j) = b(j - 1): b(j - 1) = t
End If
Next j
Next i
End Sub
抱歉,您未登录!暂时无法查看答案与解析!
登录查看答案解析试题篮
0