题目:第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行