遞迴演算法經典執行個體小結(C#實現)

來源:互聯網
上載者:User

標籤:array   溢出   sed   new   return   帶來   規模   電腦   定義   

 

一 、遞迴演算法簡介

在數學與電腦科學中,遞迴是指在函數的定義中使用函數自身的方法。
  遞迴演算法是一種直接或者間接地調用自身演算法的過程。在電腦編寫程式中,遞迴演算法對解決一大類問題是十分有效,它往往使演算法的描述簡潔而且易於理解。
遞迴演算法解決問題的特點:
  (1) 遞迴就是在過程或函數裡調用自身。
  (2) 在使用遞迴策略時,必須有一個明確的遞迴結束條件,稱為遞迴出口。
  (3) 遞迴演算法解題通常顯得很簡潔,但遞迴演算法解題的運行效率較低。所以一般不提倡用遞迴演算法設計程式。
  (4) 在遞迴調用的過程當中系統為每一層的返回點、局部量等開闢了棧來儲存。遞迴次數過多容易造成棧溢出等。所以一般不提倡用遞迴演算法設計程式。在實際編程中尤其要注意棧溢出問題。

  藉助遞迴方法,我們可以把一個相對複雜的問題轉化為一個與原問題相似的規模較小的問題來求解,遞迴方法只需少量的程式就可描述出解題過程所需要的多次重複計算,大大地減少了程式的代碼量。但在帶來便捷的同時,也會有一些缺點,也即:通常用遞迴方法的運行效率不高。

 

1、菲波那切數列(Fibonacci) 

  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …,總之,就是第N(N > 2)個數等於第(N - 1)個數和(N - 2)個數的和。

(1)不用遞迴時

  int[] a=new int[30];      a[0] = 1;             a[1] = 1;      int qian = a[0];        int hou = a[1];      for (int i = 2; i < a.Length; i++)      {                a[i] = qian + hou;                qian = hou;                hou = a[i];        }                 Console.WriteLine(a[6]);
View Code

 

(2)使用遞迴時

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 練習作業{    class Program    {        static void Main(string[] args)        {            //調用方法           int a = Foo(30);           Console.WriteLine(a);           Console.ReadLine();        }        //方法        public static int Foo(int i)        {            if (i <= 0)            {                return 0;            }            else if (i > 0 && i <= 2)            {                return 1;            }            else            {                return Foo(i - 2) + Foo(i - 1);            }        }    }}
View Code

 

2、階乘

階乘(!)是小於某個數的所有正整數的乘積。
0! = 1
1! = 1
2! = 2 * 1! = 2
3! = 3 * 2! = 6
...
n! = n * (n - 1)!

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 練習作業{    class Program    {        static void Main(string[] args)        {            //調用方法            int a = jiecheng(3);            Console.WriteLine(a);            Console.ReadLine();        }               //方法         public static int jiecheng(int n)        {            int sum;            if (n == 0)    //限制條件,對該方法調用自己做了限制               {                sum = 1;                           }            else            {                sum=n * jiecheng(n - 1);                          }            return sum;        }    }}
View Code

 

3、漢諾塔問題

 

 有三根杆子A,B,C。A杆上有N個(N>1)穿孔圓盤,盤的尺寸由下到上依次變小。要求按下列規則將所有圓盤移至C杆:
  1、每次只能移動一個圓盤;
  2、大盤不能疊在小盤上面。
  提示:可將圓盤臨時置於B杆,也可將從A杆移出的圓盤重新移回A杆,但都必須遵循上述兩條規則。
  問:如何移?最少要移動多少次?

 

         static void Main(string[] args)        {            Hanoi(5, ‘A‘, ‘B‘, ‘C‘);            Console.ReadLine();        }        public static void Hanoi(int n ,char A, char B, char C)        {            //漢諾塔問題            //將n個盤子從A座藉助B座,移到C座            if (n == 1) Move(A, C);            else            {                Hanoi(n - 1, A, C, B);                Move(A, C);                Hanoi(n - 1, B, A, C);            }        }        public static void Move(char startPlace, char endPlace)        {            Console.WriteLine("Move {0} To {1}",startPlace,endPlace);        }
View Code

 

 

4、尋找檔案

public ArrayList al=new ArrayList();   //我把ArrayList當成動態數組用,非常好用  public void GetAllDirList(string strBaseDir)  {     DirectoryInfo di=new DirectoryInfo(strBaseDir);     DirectoryInfo[] diA=di.GetDirectories();     for(int i=0;i<diA.Length;i++)     {      al.Add(diA[i].FullName);   //diA[i].FullName是某個子目錄的絕對位址,把它記錄在ArrayList中      GetAllDirList(diA[i].FullName);   //注意:這裡使用C#遞迴的方法        }  }    
View Code

 

遞迴演算法經典執行個體小結(C#實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.