Greedy algorithm in python - find the minimum waiting time of customers

1. Suppose n customers are waiting for a service at the same time. The service time required by customer i is ti(1<=i<=n). How to arrange the service order of n customers so that the total waiting time of customers can be minimized?


n=int(input('请输入顾客的位数: '))

times=[]
for i in range(n):
    time=int(input(f'请输入顾客{
      
      i+1}的服务时间: '))
    times.append(time)
times.sort()
total_time=0
for i, time in enumerate(times):
    total_time += time
    print(f'服务顾客{
      
      i+1},等待时间为{
      
      total_time}')

print(f'总的等待时间为{
      
      total_time}分钟')


案例:
请输入顾客的位数: 5
请输入顾客1的服务时间5
请输入顾客2的服务时间3
请输入顾客3的服务时间1
请输入顾客4的服务时间5
请输入顾客5的服务时间8
服务顾客1,等待时间为1
服务顾客2,等待时间为4
服务顾客3,等待时间为9
服务顾客4,等待时间为14
服务顾客5,等待时间为22
总的等待时间为22分钟

进程已结束,退出代码0



Guess you like

Origin blog.csdn.net/m0_74459049/article/details/134000015