【Python实现】魔兽世界之一:备战

class Headquter:
    ntime = 0

    """基地的名称, 能量值, 生产武士的顺序 """
    def __init__(self, name, power, order):
        self.name = name
        self.power = power
        self.order = order
        self.warroirs = [] #放入生产出来的武士
        #每一种武士的种类
        self.kind_numbers = [0 for i in range(len(self.order))]
        #当前指向生产的武士
        self.p = 0

    """生产武士"""
    def product_warroir(self):
        warroir_life = Warroirs.get_life(self.order[self.p])
        #当能量小于武士生产的最小值时,停止生产
        if(warroir_life[1] > self.power):
            print("{0:03d}".format(Headquter.ntime), self.name, "headquarter stops making warriors", sep=" ")
            return False
        #当大于时,说明可以生产
        else:
            #若当前指向武士可以生产
            if(warroir_life[0] <= self.power):
                #对基地的武士,武士种类数量,以及基地能量进行更新
                self.kind_numbers[self.p] += 1
                self.power -= warroir_life[0]
                self.warroirs.append(Warroirs(self.order[self.p]))
                warroir_name, warroir_life = self.warroirs[len(self.warroirs) - 1].get_name_life()
                #打印生产结果,注意sep要放在最后来赋值
                print("{0:03d}".format(Headquter.ntime), self.name, warroir_name,
                       str(Headquter.ntime + 1), "born with strength", str(warroir_life) + ",",
                       str(self.kind_numbers[self.p]), warroir_name, "in", self.name, "headquarter", sep=" ")
                #指向下一个武士
                self.p += 1
                self.p %= len(self.order)
                return True
            #当前武士所需能量过高,递归寻找满足条件的武士
            else:
                self.p += 1
                self.p %= len(self.order)
                return self.product_warroir()



class Warroirs:
    """设置所有武士的生命值,名称"""
    @classmethod
    def set_lifes_names(cls, lifes, names):
        cls.names = names
        cls.lifes = lifes
        cls.min_life = min(cls.lifes)

    """初始化武士,获取名称,生命值"""
    def __init__(self, id):
        self.life = Warroirs.lifes[id]
        self.name = Warroirs.names[id]

    """获取当前指向武士的生命值和所有武士的最小生命值,判断能否生产"""
    @classmethod
    def get_life(cls, id):
        return (cls.lifes[id], cls.min_life)

    """获取武士的生命值,名称"""
    def get_name_life(self):
        return (self.name, self.life)



if __name__  == "__main__":
    power = 20
    red_order = [2,3,4,1,0]
    blue_order = [3,0,1,2,4]
    red_headquter = Headquter("red", power, red_order)
    blue_headquter = Headquter("blue", power, blue_order)
    warroris_names = ("dragon", "ninja", "iceman", "lion", "wolf")
    warroris_lifes = (3, 4, 5, 6, 7)
    Warroirs.set_lifes_names(warroris_lifes, warroris_names)

    #设置标记,判断能否继续生产
    #不用red_headquter/blue_headquter.product_warroir()直接判断,是为了避免短路效应
    red_flag , blue_flag = True, True
    while True:
        if red_flag:
            red_flag = red_headquter.product_warroir()
        if blue_flag:
            blue_flag = blue_headquter.product_warroir()
        Headquter.ntime += 1
        if( not(red_flag or blue_flag)):
            break

之间写的C++实现有误,故用Python来实现一遍

这里的难点主要是两个基地的按不同顺序输出,对当前不能输出结果的跳过,按条件打印

给一些验证的输入,输出示例:

7(输入组数 )
20
3 4 5 6 7
30
40 50 60 70 80
100
10 20 30 40 50
400
20 3 300 60 98
1000
332 90 47 8989 83
5000
200 400 6 70 20
40
3 14 5 6 7
 

Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
000 blue lion 1 born with strength 6,1 lion in blue headquarter
001 red lion 2 born with strength 6,1 lion in red headquarter
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
004 blue headquarter stops making warriors
Case:2
000 red headquarter stops making warriors
000 blue headquarter stops making warriors
Case:3
000 red iceman 1 born with strength 30,1 iceman in red headquarter
000 blue lion 1 born with strength 40,1 lion in blue headquarter
001 red lion 2 born with strength 40,1 lion in red headquarter
001 blue dragon 2 born with strength 10,1 dragon in blue headquarter
002 red ninja 3 born with strength 20,1 ninja in red headquarter
002 blue ninja 3 born with strength 20,1 ninja in blue headquarter
003 red dragon 4 born with strength 10,1 dragon in red headquarter
003 blue iceman 4 born with strength 30,1 iceman in blue headquarter
004 red headquarter stops making warriors
004 blue headquarter stops making warriors
Case:4
000 red iceman 1 born with strength 300,1 iceman in red headquarter
000 blue lion 1 born with strength 60,1 lion in blue headquarter
001 red lion 2 born with strength 60,1 lion in red headquarter
001 blue dragon 2 born with strength 20,1 dragon in blue headquarter
002 red ninja 3 born with strength 3,1 ninja in red headquarter
002 blue ninja 3 born with strength 3,1 ninja in blue headquarter
003 red dragon 4 born with strength 20,1 dragon in red headquarter
003 blue iceman 4 born with strength 300,1 iceman in blue headquarter
004 red ninja 5 born with strength 3,2 ninja in red headquarter
004 blue ninja 5 born with strength 3,2 ninja in blue headquarter
005 red ninja 6 born with strength 3,3 ninja in red headquarter
005 blue ninja 6 born with strength 3,3 ninja in blue headquarter
006 red ninja 7 born with strength 3,4 ninja in red headquarter
006 blue ninja 7 born with strength 3,4 ninja in blue headquarter
007 red ninja 8 born with strength 3,5 ninja in red headquarter
007 blue ninja 8 born with strength 3,5 ninja in blue headquarter
008 red ninja 9 born with strength 3,6 ninja in red headquarter
008 blue ninja 9 born with strength 3,6 ninja in blue headquarter
009 red headquarter stops making warriors
009 blue headquarter stops making warriors
Case:5
000 red iceman 1 born with strength 47,1 iceman in red headquarter
000 blue dragon 1 born with strength 332,1 dragon in blue headquarter
001 red wolf 2 born with strength 83,1 wolf in red headquarter
001 blue ninja 2 born with strength 90,1 ninja in blue headquarter
002 red ninja 3 born with strength 90,1 ninja in red headquarter
002 blue iceman 3 born with strength 47,1 iceman in blue headquarter
003 red dragon 4 born with strength 332,1 dragon in red headquarter
003 blue wolf 4 born with strength 83,1 wolf in blue headquarter
004 red iceman 5 born with strength 47,2 iceman in red headquarter
004 blue dragon 5 born with strength 332,2 dragon in blue headquarter
005 red wolf 6 born with strength 83,2 wolf in red headquarter
005 blue ninja 6 born with strength 90,2 ninja in blue headquarter
006 red ninja 7 born with strength 90,2 ninja in red headquarter
006 blue headquarter stops making warriors
007 red iceman 8 born with strength 47,3 iceman in red headquarter
008 red wolf 9 born with strength 83,3 wolf in red headquarter
009 red ninja 10 born with strength 90,3 ninja in red headquarter
010 red headquarter stops making warriors
Case:6
000 red iceman 1 born with strength 6,1 iceman in red headquarter
000 blue lion 1 born with strength 70,1 lion in blue headquarter
001 red lion 2 born with strength 70,1 lion in red headquarter
001 blue dragon 2 born with strength 200,1 dragon in blue headquarter
002 red wolf 3 born with strength 20,1 wolf in red headquarter
002 blue ninja 3 born with strength 400,1 ninja in blue headquarter
003 red ninja 4 born with strength 400,1 ninja in red headquarter
003 blue iceman 4 born with strength 6,1 iceman in blue headquarter
004 red dragon 5 born with strength 200,1 dragon in red headquarter
004 blue wolf 5 born with strength 20,1 wolf in blue headquarter
005 red iceman 6 born with strength 6,2 iceman in red headquarter
005 blue lion 6 born with strength 70,2 lion in blue headquarter
006 red lion 7 born with strength 70,2 lion in red headquarter
006 blue dragon 7 born with strength 200,2 dragon in blue headquarter
007 red wolf 8 born with strength 20,2 wolf in red headquarter
007 blue ninja 8 born with strength 400,2 ninja in blue headquarter
008 red ninja 9 born with strength 400,2 ninja in red headquarter
008 blue iceman 9 born with strength 6,2 iceman in blue headquarter
009 red dragon 10 born with strength 200,2 dragon in red headquarter
009 blue wolf 10 born with strength 20,2 wolf in blue headquarter
010 red iceman 11 born with strength 6,3 iceman in red headquarter
010 blue lion 11 born with strength 70,3 lion in blue headquarter
011 red lion 12 born with strength 70,3 lion in red headquarter
011 blue dragon 12 born with strength 200,3 dragon in blue headquarter
012 red wolf 13 born with strength 20,3 wolf in red headquarter
012 blue ninja 13 born with strength 400,3 ninja in blue headquarter
013 red ninja 14 born with strength 400,3 ninja in red headquarter
013 blue iceman 14 born with strength 6,3 iceman in blue headquarter
014 red dragon 15 born with strength 200,3 dragon in red headquarter
014 blue wolf 15 born with strength 20,3 wolf in blue headquarter
015 red iceman 16 born with strength 6,4 iceman in red headquarter
015 blue lion 16 born with strength 70,4 lion in blue headquarter
016 red lion 17 born with strength 70,4 lion in red headquarter
016 blue dragon 17 born with strength 200,4 dragon in blue headquarter
017 red wolf 18 born with strength 20,4 wolf in red headquarter
017 blue ninja 18 born with strength 400,4 ninja in blue headquarter
018 red ninja 19 born with strength 400,4 ninja in red headquarter
018 blue iceman 19 born with strength 6,4 iceman in blue headquarter
019 red dragon 20 born with strength 200,4 dragon in red headquarter
019 blue wolf 20 born with strength 20,4 wolf in blue headquarter
020 red iceman 21 born with strength 6,5 iceman in red headquarter
020 blue lion 21 born with strength 70,5 lion in blue headquarter
021 red lion 22 born with strength 70,5 lion in red headquarter
021 blue dragon 22 born with strength 200,5 dragon in blue headquarter
022 red wolf 23 born with strength 20,5 wolf in red headquarter
022 blue ninja 23 born with strength 400,5 ninja in blue headquarter
023 red ninja 24 born with strength 400,5 ninja in red headquarter
023 blue iceman 24 born with strength 6,5 iceman in blue headquarter
024 red dragon 25 born with strength 200,5 dragon in red headquarter
024 blue wolf 25 born with strength 20,5 wolf in blue headquarter
025 red iceman 26 born with strength 6,6 iceman in red headquarter
025 blue lion 26 born with strength 70,6 lion in blue headquarter
026 red lion 27 born with strength 70,6 lion in red headquarter
026 blue dragon 27 born with strength 200,6 dragon in blue headquarter
027 red wolf 28 born with strength 20,6 wolf in red headquarter
027 blue ninja 28 born with strength 400,6 ninja in blue headquarter
028 red ninja 29 born with strength 400,6 ninja in red headquarter
028 blue iceman 29 born with strength 6,6 iceman in blue headquarter
029 red dragon 30 born with strength 200,6 dragon in red headquarter
029 blue wolf 30 born with strength 20,6 wolf in blue headquarter
030 red iceman 31 born with strength 6,7 iceman in red headquarter
030 blue lion 31 born with strength 70,7 lion in blue headquarter
031 red lion 32 born with strength 70,7 lion in red headquarter
031 blue dragon 32 born with strength 200,7 dragon in blue headquarter
032 red wolf 33 born with strength 20,7 wolf in red headquarter
032 blue ninja 33 born with strength 400,7 ninja in blue headquarter
033 red ninja 34 born with strength 400,7 ninja in red headquarter
033 blue iceman 34 born with strength 6,7 iceman in blue headquarter
034 red dragon 35 born with strength 200,7 dragon in red headquarter
034 blue wolf 35 born with strength 20,7 wolf in blue headquarter
035 red iceman 36 born with strength 6,8 iceman in red headquarter
035 blue lion 36 born with strength 70,8 lion in blue headquarter
036 red lion 37 born with strength 70,8 lion in red headquarter
036 blue iceman 37 born with strength 6,8 iceman in blue headquarter
037 red wolf 38 born with strength 20,8 wolf in red headquarter
037 blue wolf 38 born with strength 20,8 wolf in blue headquarter
038 red iceman 39 born with strength 6,9 iceman in red headquarter
038 blue iceman 39 born with strength 6,9 iceman in blue headquarter
039 red wolf 40 born with strength 20,9 wolf in red headquarter
039 blue wolf 40 born with strength 20,9 wolf in blue headquarter
040 red iceman 41 born with strength 6,10 iceman in red headquarter
040 blue iceman 41 born with strength 6,10 iceman in blue headquarter
041 red headquarter stops making warriors
041 blue headquarter stops making warriors
Case:7
000 red iceman 1 born with strength 5,1 iceman in red headquarter
000 blue lion 1 born with strength 6,1 lion in blue headquarter
001 red lion 2 born with strength 6,1 lion in red headquarter
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 14,1 ninja in blue headquarter
003 red ninja 4 born with strength 14,1 ninja in red headquarter
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
004 red dragon 5 born with strength 3,1 dragon in red headquarter
004 blue wolf 5 born with strength 7,1 wolf in blue headquarter
005 red iceman 6 born with strength 5,2 iceman in red headquarter
005 blue dragon 6 born with strength 3,2 dragon in blue headquarter
006 red headquarter stops making warriors
006 blue headquarter stops making warriors
 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值