这里提供一种可能的解决方案:

class BarberShop:
    def __init__(self, num_chairs):
        self.num_chairs = num_chairs
        self.waiting = []
        self.barber_busy = False

    def enter(self, customer):
        if len(self.waiting) < self.num_chairs:
            self.waiting.append(customer)
            print(f"{customer} entered the shop and is waiting for a haircut.")
        else:
            print(f"{customer} left the shop because there are no available chairs.")
    
    def next_customer(self):
        if self.waiting:
            self.barber_busy = True
            customer = self.waiting.pop(0)
            print(f"The barber is cutting {customer}'s hair.")
            self.barber_busy = False
        else:
            print("The barber is sleeping because there are no customers.")
    
    def is_barber_busy(self):
        return self.barber_busy

class Customer:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

if __name__ == '__main__':
    shop = BarberShop(3)
    customers = [Customer("Alice"), Customer("Bob"), Customer("Charlie"), Customer("Dave"), Customer("Eve")]
    for customer in customers:
        shop.enter(customer)
        if not shop.is_barber_busy():
            shop.next_customer()

这个代码实现了一个理发店的基本功能。当顾客进入店铺时,如果有位置,他们会等待理发师。如果没有位置,他们会离开。当理发师有空闲时,他将为下一个等待的顾客理发。如果没有等待的顾客,理发师会休息。这个代码可以通过调整等待室中的椅子数量来适应不同的店铺


原文地址: https://www.cveoy.top/t/topic/ffmu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录