输入: 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
import java.io.IOException; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; // 题目中给定的类 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; } } class Dog extends Pet{ public Dog(int x) { super("dog", x); } } class Cat extends Pet{ public Cat(int x){ super("cat", x); } } // 可以为不同的实例盖上时间戳的新的类 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(); } } // 猫狗队列 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().getCount() < this.catQ.peek().getCount()) { 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 class Main{ public static void main(String[] args) throws IOException { DogCatQueue dogCatQueue = new DogCatQueue(); Pet pet = null; StringBuilder res = new StringBuilder(); Scanner sc = new Scanner(System.in); 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")){ dogCatQueue.add(new Cat(num)); }else if(who.equals("dog")){ dogCatQueue.add(new Dog(num)); }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); } }