生兔子的問題(斐波那契數列)

來源:互聯網
上載者:User

標籤:ar   for   sp   on   問題   ad   bs   line   as   

 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();

 

生兔子的問題(斐波那契數列)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.