有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。
输入描述
给定一个操作序列代表所有事件。若第一个元素为 1 ,则代表有动物进入收容所,第二个元素为动物的编号,正数代表狗,负数代表猫;若第一个元素为 2 ,则代表有人收养动物,第二个元素若为 0 ,则采取第一种收养方式,若为 1 ,则指定收养狗,若为 -1 则指定收养猫。
输出描述
请按顺序返回收养的序列。若出现不合法的操作,即没有可以符合领养要求的动物,则将这次领养操作忽略。
示例1:
输入:
1 1
1 -1
2 0
2 -1
输出:
1 -1
#include
#include
using namespace std;
int main()
{
queueanimal;
int first, second;
while (cin >> first >> second) {
if (first == 1) {
animal.push(second);
}
if (first == 2) {
if (second == 0) {
cout << animal.front() << " ";
animal.pop();
}
if (second == 1) {
while (animal.front() < 0) {
animal.pop();
}
cout << animal.front();
}
if (second == -1) {
while (animal.front() > 0) {
animal.pop();
}
cout << animal.front();
}
}
}
}
输入第三行的时候直接输出结果了
全输入完之后再一起输出
输入第三行的时候直接输出结果其实是没有问题的
因为你这是在手工输入,输入和输出内容是混在一起的。
如果是在pta网站上测试时。是以文件的方式输入,输入和输出内容就是分开的。
如果要在手工输入时,让输入和输出内容就是分开的。
可以用数组保存输出结果,全部输入完毕后输出。
你题目的解答代码如下:
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int a[1000],la=0;
queue<int> animal;
int first, second;
while (cin >> first >> second)
{
if (first == 1)
{
animal.push(second);
}
if (first == 2)
{
if (second == 0)
{
a[la++] = animal.front();
animal.pop();
}
if (second == 1)
{
while (animal.front() < 0)
{
animal.pop();
}
a[la++] = animal.front();
}
if (second == -1)
{
while (animal.front() > 0)
{
animal.pop();
}
a[la++] = animal.front();
}
}
}
for (int i = 0; i < la; i++)
{
cout << a[i] << " ";
}
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!