图 a
小李编写VB程序实现上述功能:在列表框List1中按货物顺序显示其重量和货架号,在文本框Text1中输入机器人送货趟数,单击“结果”按钮Command1后,标签Label3和Label4中分别显示要求机器人可承受的最大载重最小值和送货总行程。程序运行界面如图b所示。
图 b
请回答下列问题:
Const num = 8 '货物数量
Const shelf = 10 '货架数量,1-9号货架可供存储货物,第10号货架用作机器人装货点
Dim g(1 To num) As Integer '各货物的重量
Dim s(1 To num) As Integer '各货物的货架号
Private Sub Form_Load()
'从数据库中读入num件物品的重量、货架号,分别存储在数组g、s中
'将各物品数据显示在list1中,代码略。
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer, n As Integer, max As Integer, total As Integer
Dim head As Integer, tail As Integer, dis As Integer, part As Integer
total = g(1)
For i = 2 To num
If g(i) > max Then max = g(i)
total = total + g(i)
Next i
n = 0
For i = max To total
part = 0: n = 0: head = 1: dis = 0
For j = 1 To num
part = part + g(j)
If part > i Then
n = n + 1: j = j - 1: tail = j
dis = dis + tran(head, tail)
part = 0: head = tail + 1
End If
Next j
If part > 0 Then
n = n + 1
dis = dis + tran(head, tail)
End If
If n <= Val(Text1.Text) Then Exit For
Next i
Label3.Caption = "机器人可承受的最大载量至少为:" + Str(i)
Label4.Caption = "机器人完成各趟任务的总行程为:" + Str(dis)
End Sub
Function tran(p As Integer, q As Integer) As Integer '计算机器人每趟按装货倒序配送并回到A 处的行程
Dim temp As Integer, t1 As Integer, t2 As Integer, k As Integer
temp = 0: t2 = shelf
For i = q To p Step -1
t1 = t2: t2 = s(i)
k =
If k > (shelf + 1) \ 2 Then k = shelf - k
temp = temp + k
Next i
If t2 <= shelf \ 2 Then temp = temp + t2 Else temp = temp + shelf - t2
tran = temp
End Function