今天老师出了道题目,大大们帮实现下:
  有一单性繁殖细胞,4天后繁殖出一个新细胞,以后每天繁殖一个。依次类推,20天后有多少个细胞?
  代码如何下,麻烦大大们贴出下!

解决方案 »

  1.   

    为什么要递归?
    using System;
    using System.Collections.Generic;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // First generate frist ear TheCell
                Chaos.Genesis();
                int timeGoes = 1;
                while (timeGoes <= 20)
                {
                    Console.WriteLine("The " + timeGoes.ToString() + " Day");
                    Chaos.LiveOneDay();
                    Console.WriteLine("\tThe total TheCell Count is: " + Chaos.CountTheCell());
                    timeGoes++;
                }            Console.WriteLine("The 20th day, total cell count is "  + Chaos.CountTheCell() + Environment.NewLine + "Press enter key quit.");
                Console.ReadLine();
            }
        }    public static class Chaos
        {
            private static int DayTick = 0;
            private static List<TheCell> AllTheCells = new List<TheCell>();
            private static List<TheCell> NewBreadedCells = new List<TheCell>();        public static void Genesis()
            {
                AllTheCells.Add(new TheCell());
            }        public static void LiveOneDay()
            {
                foreach (TheCell cell in AllTheCells)
                {
                    cell.Live();
                    if (cell.CanBread)
                        NewBreadedCells.Add(new TheCell());
                }
                AllTheCells.AddRange(NewBreadedCells);
                NewBreadedCells.Clear();
                
                DayTick++;
            }        public static int CountTheCell()
            {
                return AllTheCells.Count;
            }
        }    public class TheCell
        {
            int _age = 0;        public void Live()
            {
                _age++;
            }        public  bool CanBread
            {
                get { return _age > 4; }
            }
        }
    }