Stack_Queue 猫狗队列问题 Cat dog queue @CareerCup

【程序员代码面试指南 编程笔记】猫狗队列(C++解法) 题目 原题目使用Java作为示范,本文章提供C++解法。 猫狗两个类的定义如下public class Pet { private String type; public Pet(String type){ this.type = type; } public String getPetType(){ re 阅读详情

An animal shelter holds only dogs and cats, and operates ona strictly “first in, first out” basis. People must adopt either the “oldest”(based on arrival time) of all animals at the shelter, or they can selectwhether they could prefer a dog or a cat (and will receive the oldest animal ofthat type). They cannot select which specific animal they would like. Createthe data structures to maintain this system and implement operations such asenqueue, dequeueAny, dequeueDog and dequeueCat. You may use the built-inLinkedList data structure.


大意是有一个动物收养站,猫或狗都不停地被收养进来,同时也有人想领养。规则是领养人可以挑选猫或狗,但不能挑具体哪一只,只能挑选早被收养的那只动物。


思路:

分别建猫和狗两个队列就搞定了。



package Stack_Queue;

import java.util.LinkedList;

public class S3_7 {

	// ============================== Animal
	static abstract class Animal {
		private int order; 
		protected String name;
		public Animal(String n) {
			name = n;
		}

		public abstract String name();

		public void setOrder(int ord) {
			order = ord;
		}

		public int getOrder() {
			return order;
		}

		public boolean isOlderThan(Animal a) {
			return this.order < a.getOrder();
		}
	}
	
	// ============================== Cat
	static class Cat extends Animal {
		public Cat(String n) {
			super(n);
		}

		public String name() {
			return "Cat: " + name;
		}
	}
	
	static class Dog extends Animal {
		public Dog(String n) {
			super(n);
		}

		public String name() {
			return "Dog: " + name;
		}
	}
	
	// ============================== AnimalQueue
	static class AnimalQueue {
		LinkedList<Dog> dogs = new LinkedList<Dog>();
		LinkedList<Cat> cats = new LinkedList<Cat>();
		private int order = 0;

		public void enqueue(Animal a) {
			a.setOrder(order);
			order++;
			if (a instanceof Dog) {
				dogs.addLast((Dog) a);
			} else if (a instanceof Cat) {
				cats.addLast((Cat)a);
			}
		}

		public Animal dequeueAny() {
			if (dogs.size() == 0) {
				return dequeueCats();
			} else if (cats.size() == 0) {
				return dequeueDogs();
			}
			Dog dog = dogs.peek();
			Cat cat = cats.peek();
			if (dog.isOlderThan(cat)) {
				return dogs.poll();
			} else {
				return cats.poll();
			}
		}

		public Animal peek() {
			if (dogs.size() == 0) {
				return cats.peek();
			} else if (cats.size() == 0) {
				return dogs.peek();
			}
			Dog dog = dogs.peek();
			Cat cat = cats.peek();
			if (dog.isOlderThan(cat)) {
				return dog;
			} else {
				return cat;
			}
		}

		public int size() {
			return dogs.size() + cats.size();
		}

		public Dog dequeueDogs() {
			return dogs.poll();
		}

		public Dog peekDogs() {
			return dogs.peek();
		}

		public Cat dequeueCats() {
			return cats.poll();
		}

		public Cat peekCats() {
			return cats.peek();
		}
	}
	
	
	public static void main(String[] args) {
		AnimalQueue animals = new AnimalQueue();
		animals.enqueue(new Cat("Callie"));
		animals.enqueue(new Cat("Kiki"));
		animals.enqueue(new Dog("Fido"));
		animals.enqueue(new Dog("Dora"));
		animals.enqueue(new Cat("Kari"));
		animals.enqueue(new Dog("Dexter"));
		animals.enqueue(new Dog("Dobo"));
		animals.enqueue(new Cat("Copa"));

		System.out.println(animals.dequeueAny().name());	
		System.out.println(animals.dequeueAny().name());	
		System.out.println(animals.dequeueAny().name());	

		animals.enqueue(new Dog("Dapa"));
		animals.enqueue(new Cat("Kilo"));

		while (animals.size() != 0) {
			System.out.println(animals.dequeueAny().name());	
		}
	}
}





西门子S7-1500 PLC模块化编程与生产线自动化实践 PLC(可编程逻辑控制器)作为工业自动化核心设备,其模块化编程技术能显著提升产线控制系统的开发效率。通过SCL高级语言实现算法封装、功能块(FB)复用等工程实践,可构建稳定可靠的分层式控制架构。本文以西门子TIA Portal平台为例,详解S7-1500系列PLC在汽车零部件产线中的标准化应用方案,包含配方管理、设备联动等典型场景的SCL实现,以及Profinet通信、虚拟轴同步等进阶技巧。特别适用于需要快速实施中大型生产线自动化改造的工程师,提供经过验证的异常处理机制和模块化设计规范。 阅读详情

相关推荐

Docker 磁盘占用过多问题处理过程记录

Docker 磁盘爆满问题处理

MR.骑士道 3554

猫狗队列(python)

# 猫狗队列 class Pet: def __init__(self, type): self._type = type def getPetType(self): return self._type class Dog(Pet): def __init__(self): super().__init__("d...

qq_40431898的博客 433

SAP Business One自定义表与字段实战:从零构建企业专属数据模型

本文详细介绍了如何在SAP Business One中创建自定义表与字段,从零构建企业专属数据模型。通过实战案例和避坑指南,帮助用户掌握业务适配、流程优化和权限隔离等关键技巧,提升系统灵活性和数据管理效率。特别适合需要扩展标准功能的SAP Business One用户。

weixin_28730927的博客 362

猫狗队列问题

1、猫狗队列问题 宠物、狗和猫的类如下 class Pet{ private String type; public Pet(String type){ this.type=type; } public String getType(){ return this.type; } } class Dog extends Pe...

漫步云端 739

Xilinx Chip2Chip IP核实战:Aurora PHY通信配置与仿真避坑指南

本文深入解析Xilinx Chip2Chip IP核与Aurora PHY的通信配置与仿真优化,提供实战避坑指南。通过AXI协议转换桥实现FPGA间高速数据传输,详细讲解Aurora PHY参数设置、仿真加速技巧及常见问题解决方案,帮助工程师快速部署稳定高效的芯片间通信系统。

g5h6i7j的博客 1006

Wireshark 4.6 TCP 流图解析:3种拥塞控制算法可视化对比

本文深入解析Wireshark 4.6的TCP流图功能,通过可视化对比Reno、Cubic和BBR三种主流拥塞控制算法的性能特征。文章提供详细的实验环境搭建方法、流图分析工具使用技巧,以及算法调优建议,帮助网络工程师优化TCP性能。

weixin_29291573的博客 446

猫狗队列问题-python3

* 猫狗队列 : * 用户可以调用add方法将cat类或dog类的 实例放入队列中; * 用户可以调用pollAll方法,将队列中所有的实例按照进队列 的先后顺序依次弹出; * 用户可以调用pollDog方法,将队列中dog类的实例按照 进队列的先后顺序依次弹出; * 用户可以调用pollCat方法,将队列中cat类的实 例按照进队列的先后顺序依次弹出; * 用户可以调用isEmpt...

xujiao1179的博客 606

路径搜索算法——RRT*算法

RRT算法是RRT算法的优化版本,通过引入路径优化机制实现渐进最优性。该算法在高维空间和复杂环境中表现优异,主要步骤包括随机采样、树扩展和邻近节点重新连接。相比RRT,RRT能够不断优化路径质量,但计算复杂度更高。Python实现展示了其核心逻辑:随机采样扩展树节点,检查碰撞,选择最优父节点,并通过重新连接优化路径。算法适用于机器人导航等场景,但需平衡计算效率与路径质量。

swaggytiger的博客 3061

leetcode面试题:动物收容所(考查对队列的理解和运用)

1.执行enqueue(0,0),收容编号为0的猫咪,无返回值 2.执行enqueue(1,0),收容编号为1的猫咪,无返回值 3.执行dequeueCat(),现在要收养一只猫咪,只能收养最老的,所以就是编号为0的猫咪,返回值为[0,0] 4.执行dequeueDog(),现在要收养一只小狗,但是目前收容所没有狗狗,所以返回值为[-1,-1] 5.执行dequeueAny(),随机收养,不指定猫狗,选择目前收容所中最老的动物即可,此时收容所只剩一只编号为1的猫咪,所以返回[1,0]

m0_58681055的博客 367

6、基于深度强化学习的无人机路径规划

本文介绍了一种结合人工势场法的改进DQN深度强化学习算法在无人机路径规划中的应用。通过设计合理的网络结构、状态空间、动作空间和融合引力-斥力机制的奖励函数,实现了无人机在复杂环境中的自主避障与目标导向飞行。采用轻量级神经网络提升实时性,并在Gazebo仿真环境中进行训练与测试,结果表明该算法具有快速收敛、高成功率和良好适应性。实验评估指标包括损失函数、最大Q值和任务成功率,验证了方法的有效性。文章还探讨了其在物流配送、农业植保和环境监测等领域的应用前景,并展望了多无人机协同与复杂环境适应性等未来研究方向。

html8的博客 238

嵌入式工程师成长之路(2)——电路设计

为什么你的电源模块总是发热炸机?DC/DC和LDO选型的“潜规则”你踩坑了吗? LM2596降压芯片用错电容导致输出振荡?LED恒流驱动设计让灯珠寿命缩水50%?24V转12V方案成本超预算被老板狂喷?——这些血泪教训,谁能懂得!

云轩阁 13万+

牛客网_算法初级班_Lesson3_part2_猫狗队列_转圈打印矩阵_旋转正方形矩阵_之字形打印矩阵_行列都排好序的矩阵中找数

一、猫狗队列 1、问题描述 宠物、狗和猫的类如下: public class Pet { private String type; public Pet(String type) { this.type = type; } public String getPetType() { return this.type; } } public class Dog extends Pet { public...

禾白小三撇同学- 319

基于FreeRedis实现的.NET Redis客户端缓存缓存键条件优雅过滤

在.NET开发中,使用Redis作为缓存解决方案是很常见的。FreeRedis是一个功能强大的Redis客户端库,它提供了丰富的功能和灵活性,使我们能够轻松地与Redis进行交互。在本文中,我们将探讨如何使用FreeRedis实现缓存键条件的优雅过滤,以提高缓存的效率和准确性。

RzuScala的博客 328

栈和队列(4)--猫狗队列

宠物、狗和猫的类如下:public class Pet { private String type; public Pet(String type){ this.type = type; } public String getPetType(){ return this.type; } } public class Dog extends Pet { public Dog()

hzd12368的博客 1250

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

猫狗队列求解

一道猫狗队列的题的总结

Davidddl的博客 1205

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

程序员面试金典: 9.3栈与队列 3.7猫狗动物队列

#include #include #include #include using namespace std; /* 问题:有家动物收容所只收容狗与猫,并且严格遵守“先进先出”原则。在收养该收容所的动物时,收养人只能收养所有动物中 “最老”(根据进入收容所的时间长短)的动物,或者,可以挑选猫或狗(同时必须收养此类动物中“最老”的)。换言之,收养人不能 自由挑选

qingyuanluofeng的专栏 1616

1栈和队列--猫狗队列

//宠物、猫、狗的类 public static class Pet { private String type; public Pet(String type) { this.type = type; } public String getPetType() { return t

朵朵_calm 587

程序员代码面试指南 python实现(第一章 栈和队列 :猫狗队列)

程序员代码面试指南 python实现(第一章 栈和队列:猫狗队列 ...

Avery123123的博客 470

1.4猫狗队列

题目 实现猫狗队列 代码实现 //宠物类 class Pet { private String type; public Pet(String type) { this.type = type; } public String getPetType() { return this.type; } } //狗类 cla...

Forat 250

Codeforces Round #249 (Div. 2) A. Queue on Bus Stop

A. Queue on Bus Stop time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It's that time of the year when the Rus

flyljg的专栏 811

第四题:猫狗队列问题

题目要求: 现有猫狗类如下: class Pet{ private String type; public Pet(String type){ this.type = type; } public String getType() { return type; } } class Dog extends Pet{ ...

isea_you's blog 569

猫狗队列问题 -- 基本算法 包含作者分析过程

package basic_class_03; import java.util.LinkedList; import java.util.Queue; /** * 猫狗队列 : * 用户可以调用add方法将cat类或dog类的 实例放入队列中; * 用户可以调用pollAll方法,将队列中所有的实例按照进队列 的先后顺序依次弹出; * 用户可以调用pollDog方法,将队列中...

汤小萌的博客 664

栈和队列 猫狗队列

【题目】 宠物,猫,和狗的类如下 class Pet{ private String type; Pet(String type) { this.type = type; } public String getPet(){ return type; } } class Dog extends Pet{ Dog(String type){ super(type); }

海向的博客 413

C++队列queue基本操作

http://blog.youkuaiyun.com/cindywry/article/details/51919282C++队列queue模板类的定义在&lt;queue&gt;头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型。C++队列Queue是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。C++队列...

菜菜鸟的博客 3万+
上一篇: Stack_Queue 把栈排序 Sort a stack @CareerCup
下一篇: Tree_Graph 判断是否平衡二叉树 @CareerCup
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值