首页 > 试题广场 >

猫狗队列

[编程题]猫狗队列
  • 热度指数:3544 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
实现一种猫狗队列的结构,要求如下:
1. 用户可以调用 add 方法将 cat 或者 dog 放入队列中
2. 用户可以调用 pollAll 方法将队列中的 cat 和 dog 按照进队列的先后顺序依次弹出
3. 用户可以调用 pollDog 方法将队列中的 dog 按照进队列的先后顺序依次弹出
4. 用户可以调用 pollCat 方法将队列中的 cat 按照进队列的先后顺序依次弹出
5. 用户可以调用 isEmpty 方法检查队列中是否还有 dog 或 cat
6. 用户可以调用 isDogEmpty 方法检查队列中是否还有 dog
7. 用户可以调用 isCatEmpty 方法检查队列中是否还有 cat

输入描述:
第一行输入一个整数 n 表示 用户的操作总次数。

以下 n行 每行表示用户的一次操作

每行的第一个参数为一个字符串 s,若 s = “add”, 则后面接着有 “cat x”(表示猫)或者“dog x”(表示狗),其中的 x 表示猫狗的编号。


输出描述:
对于每个操作:

若为 “add”,则不需要输出。

以下仅列举几个代表操作,其它类似的操作输出同理。

若为 “pollAll”,则将队列中的 cat 和 dog 按照进队列的先后顺序依次弹出。(FIFO),格式见样例。

若为 "isEmpty",则检查队列中是否还有 dog 或 cat, 为空则输出 “yes”, 否则输出 “no”。
示例1

输入

11
add cat 1
add dog 2
pollAll
isEmpty
add cat 5
isDogEmpty
pollCat
add dog 10
add cat 199
pollDog
pollAll

输出

cat 1
dog 2
yes
yes
cat 5
dog 10
cat 199

备注:

保证每个猫和狗的编号x都不相同且
保证没有不合法的操作
# 存放动物的类
class Pet:
    def __init__(self, type, id):
        # 动物类型
        self.type = type
        # 动物编号
        self.id = id

    def getType(self):
        return self.type

    def getID(self):
        return self.id


# 带有时间戳的动物
class PetWithTime:
    def __init__(self, pet: Pet, time):
        self.pet = pet
        self.time = time

    def getPet(self):
        return self.pet

    def getTime(self):
        return self.time

    def getPetType(self):
        return self.pet.type


# 猫狗队列
class CatDogQueue:
    def __init__(self):
        # 分别存放猫,狗的队列
        self.dog_queue = []
        self.cat_queue = []
        self.dog_num = 0
        self.cat_num = 0
        # 时间戳
        self.time = 0

    # add操作
    def add(self, pet):
        self.time += 1
        # 加入时间戳
        pet_with_time = PetWithTime(pet, self.time)
        if pet_with_time.getPetType() == 'dog':
            self.dog_queue.append(pet_with_time)
            self.dog_num += 1
        else:
            self.cat_queue.append(pet_with_time)
            self.cat_num += 1

    # 出队操作
    def pollAll(self):
        while self.dog_num > 0 and self.cat_num > 0:
            # 选择时间戳小的弹出队列
            if self.dog_queue[0].getTime() < self.cat_queue[0].getTime():
                pet = self.dog_queue.pop(0).getPet()
                print('{} {}'.format(pet.getType(), pet.getID()))
                self.dog_num -= 1
            else:
                pet = self.cat_queue.pop(0).getPet()
                print('{} {}'.format(pet.getType(), pet.getID()))
                self.cat_num -= 1
        self.pollDog()
        self.pollCat()

    def pollCat(self):
        while self.cat_num > 0:
            pet = self.cat_queue.pop(0).getPet()
            print('{} {}'.format(pet.getType(), pet.getID()))
            self.cat_num -= 1

    def pollDog(self):
        while self.dog_num > 0:
            pet = self.dog_queue.pop(0).getPet()
            print('{} {}'.format(pet.getType(), pet.getID()))
            self.dog_num -= 1

    def isEmpty(self):
        if self.dog_num == 0 and self.cat_num == 0:
            print('yes')
        else:
            print('no')

    def isCatEmpty(self):
        if self.cat_num == 0:
            print('yes')
        else:
            print('no')

    def isDogEmpty(self):
        if self.dog_num == 0:
            print('yes')
        else:
            print('no')


# 操作次数
n = int(input())
# 猫狗队列
cat_dog_queue = CatDogQueue()
for i in range(n):
    operator = input().split()
    if operator[0] == 'add':
        pet = Pet(operator[1], operator[2])
        cat_dog_queue.add(pet)
    elif operator[0] == 'pollAll':
        cat_dog_queue.pollAll()
    elif operator[0] == 'pollDog':
        cat_dog_queue.pollDog()
    elif operator[0] == 'pollCat':
        cat_dog_queue.pollCat()
    elif operator[0] == 'isEmpty':
        cat_dog_queue.isEmpty()
    elif operator[0] == 'isDogEmpty':
        cat_dog_queue.isDogEmpty()
    elif operator[0] == 'isCatEmpty':
        cat_dog_queue.isCatEmpty()

《程序员代码面试指南》Python题解 https://zhuanlan.zhihu.com/p/444550262

发表于 2021-12-16 11:54:26 回复(0)

问题信息

上传者:小小
难度:
1条回答 7881浏览

热门推荐

通过挑战的用户

查看代码
猫狗队列