递归算法

1.  如下一串数字 : 2、6、12、20、30、42 …   请利用递归算法,算出该数列的第 20 位数

   class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Foo(20));
        }
 
        static int Foo(int index)
        {
            if (index <= 0)
                return 0;
            if (index == 1)
                return 2;
            return Foo(index - 1) + index * 2;
        }
    }

   实现过程        
       数位              数值                            递归方法
         1.                   2                    Foo(1) = 2
         2.                   6                    Foo(2) = Foo(2 – 1) + 2 * 2
         3.                  12                   Foo(3) = Foo(3 – 1) + 3 * 2       
         4.                  20                   Foo(4) = Foo(4 – 1) + 4 * 2
         5.                  30                   Foo(5) = Foo(5 – 1) + 5 * 2 

2.  使用递归算法,算出斐波那契数列(1、1、2、3、5、8、13、21、34 … )的第20位数

   class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Foo(20));
        }
 
        static int Foo(int index)
        {
            if (index <= 0)
                return 0;
            if (index == 1 || index == 2)
                return 1;
            return Foo(index - 1) + Foo(index - 2);
        }
    }

实现过程        
    数位              数值                              递归方法
      1.                   1                    Foo(1) = 1
      2.                   1                    Foo(2) = 1
      3.                   2                    Foo(3) = Foo(3 – 1) + Foo(3 – 2)    
      4.                   3                    Foo(4) = Foo(4 – 1) + Foo(4 – 2)
      5.                   5                    Foo(5) = Foo(5 – 1) + Foo(5 – 2) 

3.  使用递归算法,算出大衍’数列(0、2、4、8、12、18、24、32、40、50 … )的第20位数

   class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Foo(20));
        }
 
        static int Foo(int index)
        {
            if (index == 1)
                return 0;
            if (index == 2)
                return 2;
            return Foo(index - 1) + (index / 2) * 2;
        }
    }

实现过程
    数位              数值                         递归方法                                                注解
       1                    0                   Foo(1) = 0
       2                    2                   Foo(2) = 2
       3                    4                   Foo(3) = Foo(3 – 1) + (3 / 2 ) * 2          ps:此时 3 / 2  = 1,在程序中得到的值会取整 
       4                    8                   Foo(4) = Foo(4 – 1) + (4 / 2 ) * 2      
       5                   12                  Foo(5) = Foo(5 – 1) + (5 / 2 ) * 2          ps:同上,5 / 2 = 2,不会四舍五入
 

总结:

      1. 递归算法和我们高中学的代数函数有点类似,是比较常见的一种编程思想

      2. 寻找递归算法的规律,使用后一个数值 减去 前一个数值,根据 两个数值之差 和 数位(index) ,来寻找规律
              如数列: 2、6、12、20、30、42 …
                 数位(Index)         数值 (Value)               差值(D-value)
                       1.                         2                                     2 – 0 = 2
                       2.                         6                                     6 – 2 = 4
                       3                         12                                  12 – 6 = 6
                       4                         20                                  20 – 12 = 8
                       5                         30                                  30 – 20 = 10
                       6                         42                                  42 – 30 = 12
             此时我们会发现  D-value 刚刚好为 Index 的 2 倍 ,所以就有:
               数位              数值                           递归方法
                 1.                   2                    Foo(1) = 2
                 2.                   6                    Foo(2) =  2 + 2 * 2  = Foo(2 – 1) + 2 * 2
                 3.                  12                   Foo(3) =  6 + 3 * 2  = Foo(3 – 1) + 3 * 2       
                 4.                  20                   Foo(4) = 12 + 4 * 2 = Foo(4 – 1) + 4 * 2
                 5.                  30                   Foo(5) = 20 + 5 * 2 = Foo(5 – 1) + 5 * 2  

     

点赞