使用python实现细菌繁殖的算法代码,欢迎拍砖讨论!

题目:第0天,细菌培养皿中有1个细菌,3天后,细菌开始繁殖,细菌的生命周期只有30天,不考虑其它因素,问第n天培养皿中有多少个活着的细菌,要求40行以内代码实现

我的代码如下:

class CDay_celll(object):
    def __init__(self, day, cells):
        self.day = day
        self.cells = cells


class COne_day(object):
    def __init__(self):
        self.the_day_cells = []

    def add_cells(self, day_cell):
        self.the_day_cells.append(day_cell)

    def sum_the_day(self):
        cells = 0
        for cell in self.the_day_cells:
            cells += cell.cells
        return cells

class CCell(object):
    def __init__(self):
        self.day_cells = COne_day()
        self.day_cells.add_cells(CDay_celll(0, 1))

    def __get_next_day_cells(self, day_now):
        child_cells = 0
        next_day = COne_day()
        for cells in self.day_cells.the_day_cells:
            if day_now - cells.day < 3:  # 3 days cell
                next_day.add_cells(CDay_celll(cells.day, cells.cells))
            elif day_now - cells.day > 30:
                pass
            else:
                next_day.add_cells(CDay_celll(cells.day, cells.cells))
                child_cells += cells.cells
        if child_cells > 0:
            next_day.add_cells(CDay_celll(day_now, child_cells))
        self.day_cells = next_day
        print("day: {0}, cells: {1}".format(day_now, next_day.sum_the_day()))
        next_day.print()

    def count(self, days):
        for day in range(0, days + 1):
                self.__get_next_day_cells(day)
 
去掉空行及打印代码,有效代码35行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值