算法:细胞分裂

来源:互联网 发布:泰国人用淘宝吗 编辑:程序博客网 时间: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


其实是一个二叉树的结构,我觉得可以利用二叉树结构计算结果,但是我没有利用二叉树,而是使用队列来做的,方法不好,耗内存,待优化。

原创粉丝点击