zoukankan      html  css  js  c++  java
  • 兔子问题

    问题:

      有人想知道一对兔子可繁殖成多少对,便在大草原上筑了一道围墙,把一对兔子关在里面。

      已知一对兔子每一个月可以生一对小兔子,而一对小兔子出生后,第三个月开始生小兔子。兔子三岁后不再生育,之后一年死亡。

      则投放一对兔子,20年后回来草原上有多少兔子?

    程序源码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Rabbit
    {
        class Program
        {
            static void Main(string[] args)
            {
                LiveSystem system = new LiveSystem(2,15);
                List<RabbitOne> totalRabbit = system.Rabbits();
                Console.WriteLine("num:" + totalRabbit.Count);
                foreach (var one in totalRabbit)
                {
                    Console.WriteLine("id:" + one.Id + " month:" + one.Month+" parent:"+one.Parent);
                }
                Console.Read();
            }
        }
        public class RabbitOne
        {
            private int id;
            private int month;
            private bool live;
            private string parent;
    
            public RabbitOne(int id, int month, string parent)
            {
                this.id = id;
                this.month = month;
                this.live = true;
                this.parent = parent;
            }
    
            public int Id
            {
                get { return id; }
            }
            public int Month
            {
                get { return month; }
            }
    
            public void Grow()
            {
                this.month++;
                
            }
    
            public bool Live()
            {
                if (this.month > 4 * 12)
                {
                    return false;
                }
                return true;
            }
    
            public string Parent
            {
                get { return parent; }
            }
        }
    
        public class LiveSystem
        {
            private List<RabbitOne> _rabbits = new List<RabbitOne>();
            private int i = 0;
            public LiveSystem(int rabbitNum, int limitMonth)
            {
                while ((rabbitNum=rabbitNum - 1) >= 0)
                {
                    _rabbits.Add(new RabbitOne(i, 0,""));
                    i++;
                }
                Born();
                while ((limitMonth = limitMonth - 1) >= 0)
                {
                    _rabbits.ForEach(m =>m.Grow());
                    Born();
                }
            }
    
            public List<RabbitOne> Rabbits()
            {
                return _rabbits.Where(m =>m.Live()).ToList();
            }
    
            private void Born()
            {
                List<RabbitOne> canBornRabbits = _rabbits.Where(m => m.Month >= 3 && m.Month < 3 * 12).ToList();
                if (canBornRabbits.Count < 2) return;
                //优化:可用随机抽取获取2只兔子
                int canBorNum = (int) Math.Floor((double) (canBornRabbits.Count/2));
                while ((canBorNum = canBorNum - 1) >= 0)
                {
                    _rabbits.Add(new RabbitOne(i, 0, "$" + canBornRabbits[canBorNum*2].Id + "$" + canBornRabbits[canBorNum * 2+1 ].Id ));
                    i++;
                }
                
            }
        }
    }
    

    存在问题:

      1.如果年份过长,或初始兔子数量太多,会爆出内存溢出。

    如果谁有更好的实现方法,欢迎在底下评论、交流。

    如果有哪个地方实现的不好的地方,也欢迎指正。

    谢谢!  

  • 相关阅读:
    Java入门3.2---线程池
    Java入门3.1---多线程
    打开ppt报"powerpoint无法加载mathtype加载项"错误
    LATEX排版总结
    casbin 权限系统
    Go netpoll I/O 多路复用构建原生网络模型之源码深度解析
    使用winsw包装服务将nginx包装为Windows服务
    Node.js 的模块系统
    一文读懂 babel7 的配置文件加载逻辑
    vue-cli是什么?和 webpack是什么关系?
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/5591777.html
Copyright © 2011-2022 走看看