算法:细胞分裂

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=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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值