您当前的位置: 首页 > 

川川菜鸟

暂无认证

  • 3浏览

    0关注

    969博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ATM机等待时间详细代码和解释

川川菜鸟 发布时间:2020-10-11 12:49:45 ,浏览量:3

import random#导入随机模块 class ATM(): #定义ATM类对象 def init(self, maxtime=10): #初始化,定义最大操作时间10分钟 self.t_max = maxtime #maxtime传递给self.t_max

def getServCompleteTime(self, start=0):             #定义实例 getServCompleteTime
    return start + random.randint(1, self.t_max)  #返回TM当次操作结束的时间,累加下次 ATM 操作时长,范围在 [1, maxtime] 之间。

class Customers(): #定义Customers类 def init(self, n): #初始化,设置客户库中总数 self.count 为 n,初始时剩余客户数 self.left 相同。 self.count = n #传递 self.left = n

def getNextArrvTime(self, start=0, arrvtime=10):  #定义实例 getNextArrvTime()
    if self.left != 0: #如果剩余不为0
        self.left -= 1 #执行下一个,剩余量就减少1
        return start + random.randint(1, arrvtime) #返回从某个时间点 start,累加下位客户到达银行需要的时间
    else:
        return 0

def isOver(self):  #定义实例方法 isOver(),作用是判断客户库存是否清零,
    return True if self.left == 0 else False

c = Customers(100) #假设有100个客户 a = ATM() #a 表示 ATM机 wait_list = [] #等待列表 wait_time = 0 #客户总排队等候时间,第一个人初始为0 cur_time = 0 #当前时间,第一个人初始为0 cur_time += c.getNextArrvTime() #来一个人就要花费时间,所以当前时间就要增加,更新当前时间,我们调用getNextArrvtime wait_list.append(cur_time) #第一位客户到达银行后,默认自动加入等待列表

while len(wait_list) != 0 or not c.isOver(): #当排队列表不为空,或者客户库存未清零时执行操作 if wait_list[0]

关注
打赏
1665165634
查看更多评论
立即登录/注册

微信扫码登录

0.0491s