首页 > 代码库 > 生兔子的问题(斐波那契数列)

生兔子的问题(斐波那契数列)

 class Program
    {
        static void Main(string[] args)
        {/*题目:有1对幼兔,一个月长成小兔,再一个月长成成兔并生一对小兔,问24个月之后有多少对兔子
          
            int you=1,xiao=0,cheng=0,sum=0;//幼兔you=1,小兔xiao=0,成兔cheng=0,总数sum=0
            for(int i=1;i<=24;i++)
            {
                if (i == 1)//第一个月的兔子,只有1只幼兔
                {
                    you = 1;
                    xiao = 0;
                    cheng = 0;
                }
                else
                {
                    cheng = cheng + xiao;//上月的成兔+上月的小兔=(赋值)本月的成兔数
                    xiao = you;//上月的幼兔数=(赋值)本月成兔数
                    you = cheng;//本月成兔数=本月小兔数
                }
            sum = you + xiao + cheng;//幼兔数+小兔数+成兔数=(赋值)总数
            }
           Console.WriteLine("总数:"+sum);//写在循环内,会显示分布,循环外则只显示结果
           Console.ReadLine();*/


            {//题目修改:5对幼兔,2对小兔,3对成兔。3个月幼兔长成小兔,再三个月长成成兔并生下一对幼兔
                int you = 5, xiao = 2, cheng = 3, sum = 0;//幼兔you=5,小兔xiao=2,成兔cheng=3,总数sum=0
                for (int i = 1; i <= 8; i++)
                {
                    if (i == 1)//第一个月的兔子,有5只幼兔+2只小兔+3只成兔
                    
                        you = 5;
                        xiao = 2;
                        cheng = 3;
                    }
                    else
                    {
                        cheng = cheng + xiao;//上月的成兔+上月的小兔=(赋值)本月的成兔数
                        xiao = you;//上月的幼兔数=(赋值)本月成兔数
                        you = cheng;//本月成兔数=本月小兔数
                    }
                    sum = you + xiao + cheng;//幼兔数+小兔数+成兔数=(赋值)总数
                    Console.WriteLine("总数:" + sum);//写在循环内,会分步显示
                   
                }
                   Console.ReadLine();

 

生兔子的问题(斐波那契数列)