题库 高中信息

题干

挤牛奶问题:如有三个农民每天清晨5点起床,然后去牛棚分别给3头牛挤奶。第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻(总共挤了700个时刻),第二个农民在700时刻开始,在1200时刻结束(总共挤了500个时刻)。第三个农民在1500时刻开始2100时刻结束(总共挤了600个时刻)。期间最长挤奶的连续时间为900秒(从300时刻到1200时刻),而最长的无人挤奶的连续时间为300时刻(因第二个农民在1200时刻已结束,而第三个农民从1500时刻才开始,中间有300时刻没有人在挤牛奶)。相应时刻如下图所示:

下面提供的程序是,从N个农民(1<N<=5000)挤N头牛的工作时间列表,计算出最长有人在挤牛奶的时间段与最长的无人挤牛奶的时间段。
下图为两组不同数据程序运行后界面;

请在划线处填入相应的代码,使程序功能完善。
Dim a(1 To 100) As Integer '存放农民挤牛奶开始时刻
Dim b(1 To 100) As Integer '存放农民挤牛奶结束时刻
Dim n As Integer '存放农民人数
Private Sub Form_Load()
‘获取农民的人数与各个农民开始挤牛奶与结束挤牛奶的时间,并在list1中输出
‘相应的代码略
End Sub
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim t As Integer, ymax As Integer, nmax As Integer
Dim btime As Integer, etime As Integer, flag As Boolean
flag = True: i = 1
Do While i <= ________And flag
flag = False
For j = 1 To n - i
If a(j) > a(j + 1) Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
t = b(j): b(j) = b(j + 1): b(j + 1) = t
flag = True
End If
Next j
i = i + 1
Loop
btime = a(1): etime = b(1)
ymax = etime - btime: nmax = 0
For i = 2 To n
If _______________________________Then
etime = b(i)
If etime - btime > ymax Then ymax = etime - btime
ElseIf etime < a(i) Then
If a(i) - etime > nmax Then nmax = a(i) - etime
btime = a(i): etime = b(i)
End If
Next i
Label2.Caption = "最长挤牛奶时间为:" + Str(ymax) + " 时刻"
Label3.Caption = "最长无人挤牛奶时间为:" + Str(nmax) + " 时刻"
End Sub
上一题 下一题 0.99难度 填空题 更新时间:2019-04-17 09:08:51

答案(点此获取答案解析)