自娱自乐的小题目(1)

在网上看到了50个算术题,没事的时候就自娱自乐的做着玩~http://www.cnblogs.com/IT-Bear/archive/2012/05/11/2495641.html

题目1:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一

对兔子,假如兔子都不死,问每个月的兔子总数为多少? 
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... 

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test {
    class Program {
        //【程序1】 
        //题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一
        //对兔子,假如兔子都不死,问每个月的兔子总数为多少? 
        //1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
        //斐波那契数列
        static void Main(string[] args) {
            Console.WriteLine("第几个月:");
            var a=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("总数为:{0}",Count(a)); 
        }
        public static int Count(int n) {
            if (n > 2) {
                int temp = Count(n - 1) + Count(n - 2);
                if (temp > int.MaxValue) { return 0; } else {
                    return temp;
                }
            } else {
                return n > 0 ? 1 : 0;
            }
        }
    }
}

 

原文地址:https://www.cnblogs.com/socialdk/p/2518745.html