首页 > 试题广场 >

猫狗队列

[编程题]猫狗队列
  • 热度指数: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都不相同且
保证没有不合法的操作
用例是否有问题?这是用例输入和我觉得应该的输出,我的输出和这个是一样的但是预期输出却和我的不一样
发表于 2024-11-02 21:00:47 回复(1)
使用Java语言时,如果每次结果都立即输出,那么会因多次IO操作而超时,可以先用StringBuilder拼接输出,最后再一次性输出结果,缺点是占用内存较多。
发表于 2021-07-30 08:34:45 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            Deque<String> cat=new LinkedList<>();
            Deque<String> dog=new LinkedList<>();
            Deque<String> all=new LinkedList<>();
            StringBuilder sb=new StringBuilder();
            int n=sc.nextInt();
            for(int i=0;i<n;i++){
                String op=sc.next();
                switch (op){
                    case "add":
                        String who=sc.next();
                        int num=sc.nextInt();
                        if(who.equals("cat")){
                            cat.addLast("cat "+num);
                        }else{
                            dog.addLast("dog "+num);
                        }
                        all.addLast(who+" "+num);
                        break;
                    case "pollAll":
                        while(!all.isEmpty()){
                            String str=all.pop();
                            if(!cat.isEmpty()&&str.equals(cat.peekFirst())){
                                 sb.append(str).append("\n");
                                 cat.pop();
                            }else if(!dog.isEmpty()&&str.equals(dog.peekFirst())){
                                 sb.append(str).append("\n");
                                 dog.pop();
                            }
                        }
                        break;
                    case "pollDog":
                        while(!dog.isEmpty())
                            sb.append(dog.poll()).append("\n");
                        break;
                    case "pollCat":
                         while(!cat.isEmpty())
                            sb.append(cat.poll()).append("\n");
                        break;
                    case "isEmpty":
                        if(dog.isEmpty()&&cat.isEmpty())
                            sb.append("yes");
                        else
                            sb.append("no");
                        sb.append("\n");
                        break;
                    case "isDogEmpty":
                        if(dog.isEmpty())
                            sb.append("yes");
                        else
                            sb.append("no");
                        sb.append("\n");
                        break;
                    case "isCatEmpty":
                        if(cat.isEmpty())
                            sb.append("yes");
                        else
                            sb.append("no");
                        sb.append("\n");
                        break;
                }
                
            }
            System.out.println(sb.toString());
        }
        sc.close();
    }
    
}

发表于 2021-04-08 01:49:41 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

    // 题目中给定的类
    public static class Pet{
        private String type;
        private int x;

        public Pet (String type,int x){
            this.type = type;
            this.x = x;
        }

        public String getPetType(){
            return this.type;
        }

        public int getX(){
            return this.x;
        }
    }

    public static class Dog extends Pet{
        public Dog(int x) {
            super("dog", x);
        }
    }

    public static class Cat extends Pet{
        public Cat(int x){
            super("cat", x);
        }
    }

    // 可以为不同的实例盖上时间戳的新的类
    public static class PetEnterQueue{
        private Pet pet;
        private long count;

        public PetEnterQueue(Pet pet, long count) {
            this.pet = pet;
            this.count = count;
        }

        public Pet getPet() {
            return pet;
        }

        public long getCount() {
            return count;
        }

        public String getEnterPetType(){
            return this.pet.getPetType();
        }
    }

    // 猫狗队列
    public static class DogCatQueue{
        private Queue<PetEnterQueue> dogQ;
        private Queue<PetEnterQueue> catQ;
        private long count;

        public DogCatQueue() {
            this.dogQ = new LinkedList<>();
            this.catQ = new LinkedList<>();
            this.count = 0;
        }

        public void add(Pet pet){
            if (pet.getPetType().equals("dog")){
                this.dogQ.add(new PetEnterQueue(pet,this.count++));
            } else if (pet.getPetType().equals("cat")){
                this.catQ.add(new PetEnterQueue(pet,this.count++));
            } else {
                throw new RuntimeException("error, not dog&nbs***bsp;cat");
            }
        }

        // 注意弹出的不能是自己定义的PetEnterQueue
        public Pet pollAll(){
            if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()){
                if (this.dogQ.peek().count < this.catQ.peek().count){
                    return this.dogQ.poll().getPet();
                } else {
                    return this.catQ.poll().getPet();
                }
            } else if(!this.dogQ.isEmpty()){
                return this.dogQ.poll().getPet();
            } else if(!this.catQ.isEmpty()){
                return this.catQ.poll().getPet();
            } else {
                throw new RuntimeException("error,queue is empty");
            }
        }

        public Dog pollDog(){
            if (!dogQ.isEmpty()){
                return (Dog) this.dogQ.poll().getPet();
            } else {
                throw new RuntimeException("Dog queue is empty.");
            }
        }

        public Cat pollCat(){
            if (!catQ.isEmpty()){
                return (Cat) this.catQ.poll().getPet();
            } else {
                throw new RuntimeException("Cat queue is empty.");
            }
        }

        public boolean isEmpty(){
            return this.catQ.isEmpty() && this.dogQ.isEmpty();
        }

        public boolean isDogQueueEmpty(){
            return this.dogQ.isEmpty();
        }

        public boolean isCatQueueEmpty(){
            return this.catQ.isEmpty();
        }
    }

    public static void main(String[] args) throws IOException {
        DogCatQueue dogCatQueue = new DogCatQueue();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        StringBuilder res = new StringBuilder();
        Pet pet = null;

        while (n-->0){
            String[] strArr = in.readLine().split(" ");
            String ops = strArr[0];
            switch (ops){
                case "add":
                    if ("dog".equals(strArr[1])){
                        dogCatQueue.add(new Dog(Integer.parseInt(strArr[2])));
                    } else if ("cat".equals(strArr[1])){
                        dogCatQueue.add(new Cat(Integer.parseInt(strArr[2])));
                    } else {
                        throw new RuntimeException("Invalid add.");
                    }
                    break;
                case "pollAll":
                    while (!dogCatQueue.isEmpty()){
                        pet = dogCatQueue.pollAll();
                        // 用链式append没有警告!!
                        // 类型和分数中间记得加上空格
                        res.append(pet.getPetType()).append(" ").append(pet.getX()).append('\n');
                    }
                    break;
                case "pollDog":
                    while (!dogCatQueue.isDogQueueEmpty()){
                        pet = dogCatQueue.pollDog();
                        res.append("dog").append(" ").append(pet.getX()).append('\n');
                    }
                    break;
                case "pollCat":
                    while (!dogCatQueue.isCatQueueEmpty()){
                        pet = dogCatQueue.pollCat();
                        res.append("cat").append(" ").append(pet.getX()).append('\n');
                    }
                    break;
                case "isEmpty":
                    if (!dogCatQueue.isEmpty()){
                        res.append("no").append('\n');
                    } else {
                        res.append("yes").append('\n');
                    }
                    break;
                case "isDogEmpty":
                    if(!dogCatQueue.isDogQueueEmpty()){
                        res.append("no").append('\n');
                    } else {
                        res.append("yes").append('\n');
                    }
                    break;
                case "isCatEmpty":
                    if(!dogCatQueue.isCatQueueEmpty()){
                        res.append("no").append('\n');
                    } else {
                        res.append("yes").append('\n');
                    }
                    break;
            }
        }
        // 最后的输出只能由一个回车,所以是System.out.println(res.substring(0,res.length()-1));或者下面这个
        System.out.print(res);
    }
}

发表于 2020-04-04 19:18:34 回复(1)

问题信息

上传者:小小
难度:
5条回答 7880浏览

热门推荐

通过挑战的用户

查看代码
猫狗队列