算法:细胞分裂

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 queue = new LinkedBlockingDeque();
        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=1
07-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=2
07-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=4
07-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=7
07-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=13
07-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=24
07-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=44
07-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=81
07-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=149
07-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


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

你可能感兴趣的:(算法)