C++ 猫狗收容所

35 篇文章 0 订阅
/**
题目描述
有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,
第一种为直接收养所有动物中最早进入收容所的,
第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。

给定一个操作序列int[][2] ope(C++中为vector<vector<int>>)代表所有事件。
若第一个元素为1,则代表有动物进入收容所,第二个元素为动物的编号,正数代表狗,负数代表猫;
若第一个元素为2,则代表有人收养动物,
    第二个元素若为0,则采取第一种收养方式,若为1,则指定收养狗,若为-1则指定收养猫。
请按顺序返回收养的序列。若出现不合法的操作,即没有可以符合领养要求的动物,则将这次领养操作忽略。

**/
#include<iostream>
using namespace std;
#include<vector>

class CatDogAsylum {
public:
    vector<int> asylum(vector<vector<int> > ope) {
        // write code here
        vector<int> result;
        // 初始化result为vector向量,初始值为0
        // valid表示输出向量中实际应该包含的元素
        vector<int> helper_queue;
        //构造辅助队列,初始状态下,队列中没有任何元素
        for(unsigned int i=0;i<ope.size();i++){
            if(ope[i][0]==1){
                // 代表有动物进入收容所
                helper_queue.push_back(ope[i][1]);
            }
            else if(ope[i][0]==2){
                // 第一个元素为2,则代表有人收养动物
                if(ope[i][1]==0){
                    if(helper_queue.size()>0){
                        result.push_back(helper_queue[0]);
                        helper_queue.erase(helper_queue.begin());
                    }
                }
                else if(ope[i][1]==1){
                    bool flag=false; // 表示从当前的队列中还没有找到为1的元素
                    unsigned int i=0;
                    while(i<helper_queue.size()){
                        if(helper_queue[i]>0){
                            flag=true;
                            break;
                        }
                        else{
                            i++;
                        }
                    }
                    if(flag==true){
                        result.push_back(helper_queue[i]);
                        helper_queue.erase(helper_queue.begin()+i);
                    }
                }
                else if(ope[i][1]==-1){
                    bool flag=false;
                    unsigned int j=0;
                    while(j<helper_queue.size()){
                        if(helper_queue[j]<0){
                            flag=true;
                            break;
                        }
                        else{
                            j++;
                        }
                    }
                    if(flag==true){
                        result.push_back(helper_queue[j]);
                        helper_queue.erase(helper_queue.begin()+j);
                    }
                }
            }
        }

        return result;
    }
};


int main(){
    vector<vector<int> > v;
    int a1[]={1,1};
    int a2[]={1,-1};
    int a3[]={2,0};
    int a4[]={2,-1};


    vector<int> line1(a1,a1+2);
    vector<int> line2(a2,a2+2);
    vector<int> line3(a3,a3+2);
    vector<int> line4(a4,a4+2);

    v.push_back(line1);
    v.push_back(line2);
    v.push_back(line3);
    v.push_back(line4);

    vector<int> output;
    CatDogAsylum here;
    output=here.asylum(v);
    for(unsigned int i=0;i<output.size();i++){
        cout<<output[i]<<endl;
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值