s = "20210606"
c1 = 6
n = 0
For i = 1 To Len(s)
c = Val(Mid(s, i, 1))
If c < c1 Then
n = n + 1: c1 = c
Next i
运行该程序段后,n 的值为( )
s = ""
For i = 2 * n - 1 To 1 Step -1
If i <= n Then
S=
Else
S=
End If
Next i
上述程序段方框处的语句分别为:( )
For i = 1 To 6
x = Int(Rnd * 10) + 1
If i Mod 2 <> x Mod 2 Then
a(i) = x
Else
a(7 - i) = x
End If
Next i
数组a各元素的初始值为0,执行程序后a数组各元素的值不可能是( )
'生成6个随机正整数,依次存入数组a(1)到(6),代码略
Const n = 6
k = Int(Rnd * 3)
For i = 1 To 2
For j = 1 To n - i - k
If a(j) > a(j + 1) Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
执行上述程序段后,下列选项中,a(1)到a(6)各元素的值不可能是( )
图 b
图 b
排序前 |
55 |
70 |
70 |
78 |
71 |
79 |
75 |
80 |
80 |
90 |
排序后 |
55 |
70 |
70 |
71 |
75 |
78 |
79 |
80 |
80 |
90 |
查找该组数据中满足[k1, k2]范围的数据个数为:8 |
实现上述功能的VB程序如下,请回答下列问题:
Private Sub Command1_Click()
Const n = 10
Dim a(1 To n) As Integer, k As Integer
Dim i As Integer, j As Integer, k1 As Integer, k2 As Integer
Dim p As Integer, q As Integer
Dim b As Integer '查找满足条件的第一个数据所在数组元素的下标
'读取排序前的一组正整数,存储在数组a中。代码略
p= Val(Text1.Text)
q = Val(Text2.Text)
For i = p To q - 1
For j = Step -1
If a(j) < a(j - 1) Then
k = a(j): a(j) = a(j - 1): a(j - 1) = k
End If
Next j
Next i
k1 = Val(Text3.Text)
k2 = Val(Text4.Text)
i = 1: j = n
Do While i <= j
m = (i + j) \ 2
If ① Then
i = m + 1
Else
j = m - 1
End If
Loop
b = ②
i = 1: j = n
Do While i <= j
m = (i + j) \ 2
If a(m) <= k2 Then
i = m + 1
Else
j = m - 1
End If
Loop
Label1.Caption = Str ③
End Sub
① ② ③
图 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