zoukankan      html  css  js  c++  java
  • 猫狗收容所

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

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

    测试样例:
    [[1,1],[1,-1],[2,0],[2,-1]]
    返回:[1,-1]

    solution:

    # -*- coding:utf-8 -*-
    class CatDogAsylum:
        def asylum(self, ope):
            animals = []
            cats,dogs = 0,0
            res = []
            for op in ope:
                if op[0]==1:
                    if op[1]>0:
                        animals.append((1,op[1]))
                        dogs += 1
                    else:
                        animals.append((0,op[1]))
                        cats += 1
                else:
                    if op[1]==0:
                        if len(animals)==0:
                            continue
                        temp = animals.pop(0)
                        res.append(temp[1])
                        if temp[0]==1:
                            dogs -= 1
                        else:
                            cats -= 1
                    elif op[1]==1:
                        if dogs==0:
                            continue
                        dogs -= 1
                        for i in range(len(animals)):
                            if animals[i][0]==1:
                                res.append(animals.pop(i)[1])
                                break
                    else:
                        if cats==0:
                            continue
                        cats -= 1
                        for i in range(len(animals)):
                            if animals[i][0]==0:
                                res.append(animals.pop(i)[1])
                                break
            return res
    

    一遍过,超得意

  • 相关阅读:
    自动部署基于Maven的war文件到远程Tomcat
    解决Eclipse中新创建的Maven项目不自动创建web.xml文件
    JFreeChart
    hibernate
    hibernate
    hibernate
    hibernate
    Hibernate
    hibernate关联关系映射详解
    Hibernate获取数据java.lang.StackOverflowError
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/11182585.html
Copyright © 2011-2022 走看看