首页 > 试题广场 >

猫狗队列

[编程题]猫狗队列
  • 热度指数:3519 时间限制: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都不相同且
保证没有不合法的操作
分享一个go语言写法的,这道题题目很基础,输出输出格式是真的难受。
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var out = bufio.NewWriter(os.Stdout)

type Pet struct {
	name string
}

type PetEnterQueue struct {
	pet   Pet
	count int
}

type DogCatQueue struct {
	dogQ  []PetEnterQueue
	catQ  []PetEnterQueue
	count int
}

func DogCatQueueConstructor() DogCatQueue {
	return DogCatQueue{
		dogQ:  make([]PetEnterQueue, 0),
		catQ:  make([]PetEnterQueue, 0),
		count: 0,
	}
}
func (this *DogCatQueue) add(name string, id string) {
	if name == "dog" {
		this.dogQ = append(this.dogQ, PetEnterQueue{Pet{name + " " + id}, this.count})
		this.count = this.count + 1
	} else if name == "cat" {
		this.catQ = append(this.catQ, PetEnterQueue{Pet{name + " " + id}, this.count})
		this.count = this.count + 1
	}
}
func (this *DogCatQueue) pollAll() {
	if (len(this.dogQ) != 0) && (len(this.catQ) != 0) {
		if this.dogQ[0].count <= this.catQ[0].count {
			this.pollDog()
		} else {
			this.pollCat()
		}
	} else {
		if len(this.dogQ) != 0 {
			this.pollDog()
		}
		if len(this.catQ) != 0 {
			this.pollCat()
		}
	}
}
func (this *DogCatQueue) pollDog() {
	fmt.Println(this.dogQ[0].pet.name)
	this.dogQ = this.dogQ[1:]
}
func (this *DogCatQueue) pollCat() {
	fmt.Println(this.catQ[0].pet.name)
	this.catQ = this.catQ[1:]
}

func (this *DogCatQueue) isCatEmpty() bool {
	if len(this.catQ) == 0 {
		return true
	}
	return false
}

func (this *DogCatQueue) isDogEmpty() bool {
	if len(this.dogQ) == 0 {
		return true
	}
	return false
}

func (this *DogCatQueue) isEmpty() bool {
	if len(this.catQ) == 0 && len(this.dogQ) == 0 {
		return true
	}
	return false
}

var sc = bufio.NewScanner(os.Stdin)

func readLine() string {
	sc.Scan()
	return sc.Text()
}

func strToInt(str string) int {
	v, _ := strconv.Atoi(str)
	return v
}

func main() {
	nStr := readLine()
	DogCatQueue := DogCatQueueConstructor()
	n, _ := strconv.Atoi(nStr)
	for i := 0; i < n; i++ {
		str := readLine()
		splits := strings.Split(str, " ")
		if len(splits) > 1 {
			if splits[0] == "add" {
				DogCatQueue.add(splits[1], splits[2])
			}
		} else {
			switch str {
			case "pollAll":
				for !DogCatQueue.isEmpty() {
					DogCatQueue.pollAll()
				}
			case "pollDog":
				for !DogCatQueue.isDogEmpty() {
					DogCatQueue.pollDog()
				}
			case "pollCat":
				for !DogCatQueue.isCatEmpty() {
					DogCatQueue.pollCat()
				}
			case "isEmpty":
				if DogCatQueue.isEmpty() {
					fmt.Println("yes")
				} else {
					fmt.Println("no")
				}
			case "isDogEmpty":
				if DogCatQueue.isDogEmpty() {
					fmt.Println("yes")
				} else {
					fmt.Println("no")
				}
			case "isCatEmpty":
				if DogCatQueue.isCatEmpty() {
					fmt.Println("yes")
				} else {
					fmt.Println("no")
				}
			}
		}
	}

	out.Flush()
}


发表于 2022-02-22 19:56:06 回复(0)

问题信息

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

热门推荐

通过挑战的用户

查看代码
猫狗队列