思路:

  1. 创建一个栈和一个队列,分别用于代表停车场和便道。
  2. 当有车辆进入停车场时,先判断停车场是否已满,若未满则将该车辆入栈并输出停车信息;若已满则将该车辆入队列并输出等待信息。
  3. 当有车辆开出停车场时,先将其从栈中弹出并输出离开信息,再判断队列中是否有车辆等待,若有则将队首车辆入栈并输出停车信息。
  4. 重复步骤2和3,直到程序结束。

代码如下:

class ParkingLot:
    def __init__(self, capacity):
        self.capacity = capacity
        self.stack = []
        self.queue = []

    def enter(self, car):
        if len(self.stack) < self.capacity:
            self.stack.append(car)
            print(f"Car {car} parked in the parking lot.")
        else:
            self.queue.append(car)
            print(f"Car {car} is waiting in the queue.")

    def exit(self, car):
        if car in self.stack:
            self.stack.remove(car)
            print(f"Car {car} has left the parking lot.")
            if self.queue:
                next_car = self.queue.pop(0)
                self.stack.append(next_car)
                print(f"Car {next_car} parked in the parking lot.")
        else:
            print(f"Car {car} is not in the parking lot.")

    def display(self):
        print(f"Parking lot: {self.stack}")
        print(f"Queue: {self.queue}")

parking_lot = ParkingLot(5)

while True:
    command = input("Enter command (enter, exit, display, quit): ")
    if command == "enter":
        car = input("Enter car number: ")
        parking_lot.enter(car)
    elif command == "exit":
        car = input("Enter car number: ")
        parking_lot.exit(car)
    elif command == "display":
        parking_lot.display()
    elif command == "quit":
        break
    else:
        print("Invalid command.")

运行示例:

Enter command (enter, exit, display, quit): enter
Enter car number: A123
Car A123 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: B456
Car B456 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: C789
Car C789 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: D012
Car D012 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: E345
Car E345 parked in the parking lot.
Enter command (enter, exit, display, quit): enter
Enter car number: F678
Car F678 is waiting in the queue.
Enter command (enter, exit, display, quit): display
Parking lot: ['A123', 'B456', 'C789', 'D012', 'E345']
Queue: ['F678']
Enter command (enter, exit, display, quit): exit
Enter car number: C789
Car C789 has left the parking lot.
Car F678 parked in the parking lot.
Enter command (enter, exit, display, quit): exit
Enter car number: G901
Car G901 is not in the parking lot.
Enter command (enter, exit, display, quit): display
Parking lot: ['A123', 'B456', 'D012', 'E345', 'F678']
Queue: []
Enter command (enter, exit, display, quit): quit
假设停车场内只有一个可停放N假设N为5辆汽车的狭长通道且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序依次由南向北排列。若车场内已停满N辆车则后来的汽车只能在门外的便道也就是候车场等候一旦有车开走则排在候车场上的第一辆车便可开入停车场假定候车场同样只有N辆汽车的容量。当停车场内某辆汽车要开走时在它之后进入的车辆必须先退出停车场在待停区等候等该车开出大门外待停区的车辆再按原次序进入

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

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