使用python实现细菌繁殖的算法代码,欢迎拍砖讨论!
2018-02-05 15:08
791 查看
题目:第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行
相关文章推荐
- 使用python实现baidu hi自动登录的代码
- [算法] 【一段破代码,对SSL异步编程感兴趣的同学有福了, 欢迎高手指正使用错误】
- python翻译软件实现代码(使用google api完成)
- Python def函数的定义、使用及参数传递实现代码
- 使用python提取html文件中的特定数据的实现代码
- Python使用Socket(Https)Post登录百度的实现代码
- 数据挖掘之Apriori算法详解和Python实现代码分享
- 使用VC内嵌Python实现的一个代码检测工具
- python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
- Python实现的最近最少使用算法
- 使用70行Python代码实现一个递归下降解析器的教程
- 近期准备写一系列关于使用python实现常用算法的文章
- 借助apktool.jar工具,使用python代码简化批量反编译apk安装包的简单实现
- Python def函数的定义、使用及参数传递实现代码
- 通过python的import hooks实现像引用代码一样使用配置文件
- 使用VC内嵌Python实现的一个代码检测工具
- Python实现的最近最少使用算法
- Python使用Socket(Https)Post登录百度的实现代码
- 15道使用频率极高的基础算法题(完整代码实现)
- python翻译软件实现代码(使用google api完成)