C#递归例程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1   //函数的递归调用
{
//F(n)= F(n-1)+F(n-2)... F(1)=3; F(0)=2; 求F(40)  
    class Program
    {
        static int F(int n)
        {
            if (n == 0) return 2;  //递归终止的条件
            if (n == 1) return 3;
            return F(n - 1) + F(n - 2);
        }
        static void Main(string[] args)
        {
            int res1= F(40);
            Console.Write(res1);           
            int res2 = F(2);
            Console.Write(res2);
            Console.ReadKey();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/snow-zhang/p/9161562.html