编写Python程序,统计接送n个参会者所需的最少车辆数。运行程序,显示所有参会者提交的信息,按到达时间先后排列,再显示所需的最少车辆数,程序运行结果如图所示。
a = [ '08:15 4','08:14 3','08:23 4','08:15 2','08:12 2','08:17 1','08:17 3','08:19 4','08:21 4','08:17 1' ]
def tran (str1) :
ss = int ( str1[ : 2 ] ) * 60 + int ( str1 [3 : 6] )
return ss
for i in range( len ( a ) - 1) :
for j in range ( len ( a ) – 1 , i , -1) :
if a [j] < a [j-1]:
a [j] , a[j-1] = a [j-1] , a [j]
n = len ( a )
b = [ ]
c = [ ]
for i in range ( n ) :
b . append ( tran ( a [ i ] [ : 5 ] ) )
c . append ( b[ -1 ] + int ( a [ i ] [ 6: ] ) )
for j in range ( i , 0 , -1 ) :
if c[k] > c[j]:
b[k] , b[j] = b[j] , b[k]
c[k] , c[j] = c[j] , c[k]
else :
break
sum = 0
flag = [ False for i in range ( n ) ]
for i in range ( n ) :
if flag [ i ] == False:
for j in range ( i , n ) :
if: flag [ j ] = True
print ('接送所有参会者最少需要%d 辆车' %sum)