算法:细胞分裂
来源:互联网 发布:泰国人用淘宝吗 编辑:程序博客网 时间:2024/05/17 09:35
package com.bxh.algorithms.OtherAlgorithms;/** * Created by bxh on 7/26/17. */import java.util.Queue;import java.util.concurrent.LinkedBlockingDeque;/** * 一个细胞,每小时分裂成两个,分裂三次以后,本体会死掉。 * n个小时之后,有多少个细胞? */public class Cell { public static void getCellCount() { for (int i = 0; i < 10; i++) { System.out.println("cell with time -----------"); int count = new Cell().getCellCount(i); System.out.println("cell with time i=" + i + " count=" + count); } } public int getCellCount(int n) { if (n == 0) { return 1; } Queue<Node> queue = new LinkedBlockingDeque<Node>(); int spCount = 0; Node oriNode = new Node(); queue.add(oriNode); int lastNewAddCount = 1; while (spCount < n) { int newAddCount = 0; while (lastNewAddCount > 0) { lastNewAddCount --; Node curNode = queue.poll(); curNode.division += 1; if (curNode.division == 3) { queue.add(new Node()); newAddCount+=1; } else { queue.add(curNode); queue.add(new Node()); newAddCount+=2; } } lastNewAddCount = newAddCount; spCount++; } return queue.size(); } public static class Node { int division; }}
结果:
07-26 12:26:44.169 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.169 24307-24307/? I/System.out: cell with time i=0 count=107-26 12:26:44.169 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.171 24307-24307/? I/System.out: cell with time i=1 count=207-26 12:26:44.171 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.171 24307-24307/? I/System.out: cell with time i=2 count=407-26 12:26:44.171 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.171 24307-24307/? I/System.out: cell with time i=3 count=707-26 12:26:44.171 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.171 24307-24307/? I/System.out: cell with time i=4 count=1307-26 12:26:44.171 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.171 24307-24307/? I/System.out: cell with time i=5 count=2407-26 12:26:44.171 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.171 24307-24307/? I/System.out: cell with time i=6 count=4407-26 12:26:44.171 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.172 24307-24307/? I/System.out: cell with time i=7 count=8107-26 12:26:44.172 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.172 24307-24307/? I/System.out: cell with time i=8 count=14907-26 12:26:44.172 24307-24307/? I/System.out: cell with time -----------07-26 12:26:44.173 24307-24307/? I/System.out: cell with time i=9 count=274
其实是一个二叉树的结构,我觉得可以利用二叉树结构计算结果,但是我没有利用二叉树,而是使用队列来做的,方法不好,耗内存,待优化。
阅读全文
0 0
- 算法:细胞分裂
- 细胞分裂
- 细胞分裂
- 【p093】细胞分裂
- [数论/质因数分解] 细胞分裂
- 递归求解细胞分裂问题
- NKOI 1086 细胞分裂
- 洛谷 P1069 细胞分裂
- [普及] NOIP 2009 细胞分裂
- [NOIP2009普及] 细胞分裂
- codevs 1152 细胞分裂
- NOIP2009普及组-细胞分裂
- 维基oi 2952 细胞分裂 2
- Wikioi 1152 细胞分裂 (快速幂)
- vijos P1814细胞分裂 (数论)
- 2017.4.19 细胞分裂 思考记录
- 科普—精原细胞分裂
- 洛谷 P1069 细胞分裂 质因数分解
- 2017 Multi-University Training Contest 1001 (简单)(技巧)
- CodeForces
- MFC编辑框自动换行
- Ubuntu 16.04 为mysql配置utf8编码
- 杭电OJ 1003
- 算法:细胞分裂
- 第一次启动eclipse出现错误
- Hibernate查询缓存
- 关于easyUI一些标签的使用
- 组合数取模,预处理阶乘逆元模板
- android 如何通过拨号盘暗码启动你的应用
- MFC颜色对话框的使用
- 欢迎使用CSDN-markdown编辑器
- [LeetCode] 131. Palindrome Partitioning