例题5.3猫狗收容所
//还可以只定义猫狗两个队列,但是queue的数据类型为结构体animal,该结构体内部还有一个记录次序的int
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> cat;//负数
queue<int> dog;//正数
queue<int> animal;
int n;cin>>n;
int m,t;
for(int i=1;i<=n;i++)
{
cin>>m>>t;
if(m==1&&t>0)//进来狗
{
dog.push(t);
animal.push(t);
}
else if(m==1&&t<0)
{
cat.push(t);
animal.push(t);
}
else if(m==2 &&t==0)//第一种收养方式
{
if(!animal.empty())
{
int x=animal.front();//该动物的编号
cout<<x<<" ";
animal.pop();
if(x>0)//最先的动物是狗
{
dog.pop();
}
else
{
cat.pop();
}
}
}
else if(m==2 && t==1)//收养狗
{
if(!dog.empty())
{
int x=dog.front();
cout<<x<<" ";
dog.pop();
if(animal.front()==x)
{
animal.pop();
}
else
{
int count=0;//被临时后置的动物数量
while(animal.front()!=x)
{
count++;
int y=animal.front();
animal.pop();
animal.push(y);
}
animal.pop();
int countt=animal.size()-count;
for(int i=1;i<=countt;i++)
{
int y=animal.front();
animal.pop();
animal.push(y);
}
}
}
}
else if(m==2 && t==-1)//收养猫
{
if(!cat.empty())
{
int x=cat.front();
cout<<x<<" ";
cat.pop();
if(animal.front()==x)
{
animal.pop();
}
else
{
int count=0;//被临时后置的动物数量
while(animal.front()!=x)
{
count++;
int y=animal.front();
animal.pop();
animal.push(y);
}
animal.pop();
int countt=animal.size()-count;
for(int i=1;i<=countt;i++)
{
int y=animal.front();
animal.pop();
animal.push(y);
}
}
}
}
}
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<int> cat;//负数
queue<int> dog;//正数
queue<int> animal;
int n;cin>>n;
int m,t;
for(int i=1;i<=n;i++)
{
cin>>m>>t;
if(m==1&&t>0)//进来狗
{
dog.push(t);
animal.push(t);
}
else if(m==1&&t<0)
{
cat.push(t);
animal.push(t);
}
else if(m==2 &&t==0)//第一种收养方式
{
if(!animal.empty())
{
int x=animal.front();//该动物的编号
cout<<x<<" ";
animal.pop();
if(x>0)//最先的动物是狗
{
dog.pop();
}
else
{
cat.pop();
}
}
}
else if(m==2 && t==1)//收养狗
{
if(!dog.empty())
{
int x=dog.front();
cout<<x<<" ";
dog.pop();
if(animal.front()==x)
{
animal.pop();
}
else
{
int count=0;//被临时后置的动物数量
while(animal.front()!=x)
{
count++;
int y=animal.front();
animal.pop();
animal.push(y);
}
animal.pop();
int countt=animal.size()-count;
for(int i=1;i<=countt;i++)
{
int y=animal.front();
animal.pop();
animal.push(y);
}
}
}
}
else if(m==2 && t==-1)//收养猫
{
if(!cat.empty())
{
int x=cat.front();
cout<<x<<" ";
cat.pop();
if(animal.front()==x)
{
animal.pop();
}
else
{
int count=0;//被临时后置的动物数量
while(animal.front()!=x)
{
count++;
int y=animal.front();
animal.pop();
animal.push(y);
}
animal.pop();
int countt=animal.size()-count;
for(int i=1;i<=countt;i++)
{
int y=animal.front();
animal.pop();
animal.push(y);
}
}
}
}
}
return 0;
}
全部评论
相关推荐
06-03 03:20
河北农业大学 人力资源专员/助理 点赞 评论 收藏
分享