網頁

2020年4月28日 星期二

Ex8. 費氏數列Visual C#遞迴函式實作 Fibonacci sequence



1. 何謂遞迴函數?
2. 實作費氏數列,用遞迴方法

87 則留言:

  1. b10833042謝孟修2020年4月29日 上午9:34

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  2. b10833069 鍾秉哲2020年4月29日 上午9:34

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0; n<=40; n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  3. b10833148張詔明2020年4月29日 上午9:34

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  4. b10833023 陳姿茹2020年4月29日 上午9:35

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _0429
    {
    class Program
    {
    static int ff(int n)
    {
    if (n==0||n==1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  5. B10833043 彭律惠2020年4月29日 上午9:35

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if(n==0||n==1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  6. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace F
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  7. b10833033 蔡孟頻2020年4月29日 上午9:36

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n ==0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={0};", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  8. B10833082 黃鈺淇2020年4月29日 上午9:36

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _2020._04._29
    {
    class Program
    {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _2020._04._29
    {
    class Program
    {
    static int ff(int n)
    {
    if(n==0||n==1)
    return n;
    else
    return ff(n-1) + ff(n-2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0; n<=40; n++)
    Console.WriteLine("ff({0})={1}" , n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  9. b10833027呂沂真2020年4月29日 上午9:37

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0;n<=50;n++)
    Console.WriteLine("ff({0})={1}",n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  10. b10833042謝孟修2020年4月29日 上午9:38

    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)
    也就是說,當我們在 first() 裡呼叫 last() 時,需要等 last() 執行完後,才會回到 first() 繼續執行。
    另外,在寫遞回函式的時候一定記得設定終止條件,否則很容易跳不出來,導致無窮迴圈的情況產生。

    回覆刪除
  11. b10833148張詔明2020年4月29日 上午9:38

    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)
    也就是說,當我們在 first() 裡呼叫 last() 時,需要等 last() 執行完後,才會回到 first() 繼續執行。
    另外,在寫遞回函式的時候一定記得設定終止條件,否則很容易跳不出來,導致無窮迴圈的情況產生。

    回覆刪除
  12. B10833029 余柏翰2020年4月29日 上午9:38

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("費式數列");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除

  13. 1.
    我們在函數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候,必須改變它的參數,例如:

    int R(int n)

    {

    if(結束條件成立) return 預設值;

    return R( n 的運算式 );

    }


    2.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace F
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  14. B10833029 余柏翰2020年4月29日 上午9:38

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("費式數列");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  15. B10833043 彭律惠2020年4月29日 上午9:39

    遞迴(英語:Recursion),又譯為遞歸,在數學與電腦科學中,是指在函數的定義中使用函數自身的方法。遞迴一詞還較常用於描述以自相似方法重複事物的過程。例如,當兩面鏡子相互之間近似平行時,鏡中巢狀的圖像是以無限遞迴的形式出現的。也可以理解為自我複製的過程。

    回覆刪除
  16. b10833002田昌弘2020年4月29日 上午9:39

    函數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候,必須改變它的參數,例如:

    int R(int n)

    {

    if(結束條件成立) return 預設值;

    return R( n 的運算式 );

    }

    -------------------------------------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _123
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("費氏數列");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  17. B10833047馮群赫2020年4月29日 上午9:39

    說明:費式數列的前兩項為 1、1,之後的每一項為前兩項之和,即 Fn=Fn-1+Fn-2,費式數列的前 10 項為:1、1、2、3、5、8、13、21、34、55。由使用者輸入一個正數數 n ( n < 40 ),計算出費式數列的第 n 項之值並輸出之。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  18. B10833050 張少豪2020年4月29日 上午9:40

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp2
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  19. b10833024 王翔緯2020年4月29日 上午9:40

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0;n<=50; n++)
    Console .WriteLine ("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  20. 剛才我們在函數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候,必須改變它的參數,例如:

    int R(int n)

    {

    if(結束條件成立) return 預設值;

    return R( n 的運算式 );

    }
    _______________________________________________________________

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace 費式係數
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個計算費式數列的程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("ff({0})={1}",n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  21. B10833082 黃鈺淇2020年4月29日 上午9:41

    1.何謂遞迴函數?
    A:在數學和電腦科學中,遞迴指由一種(或多種)簡單的基本情況定義的一類物件或方法,並規定其他所有情況都能被還原為其基本情況。

    回覆刪除
  22. B10833026鄭亞易2020年4月29日 上午9:42

    1.
    遞迴函數就是在函數的定義裏面呼叫自己的函數。遞迴函數至少需要包含終止條件與遞迴條件兩個 部份。遞迴可以讓程式與數學定義相契合,程式碼較為簡潔易懂。遞迴可能會重複計算相同的東西多次,造成效能的浪費。
    2.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("ff({0})={1})", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  23. b10833024 王翔緯2020年4月29日 上午9:44

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0;n<=50; n++)
    Console .WriteLine ("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  24. B10833050 張少豪2020年4月29日 上午9:45

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp2
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  25. A10833001林承君2020年4月29日 上午9:45

    遞迴函數說明
    剛才我們在函數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候,必須改變它的參數,例如:

    int R(int n)

    {

    if(結束條件成立) return 預設值;

    return R( n 的運算式 );

    }

    課堂實作
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  26. b10833027呂沂真2020年4月29日 上午9:45

    1.
    剛才我們在函數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候
    2.using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0;n<=50;n++)
    Console.WriteLine("ff({0})={1}",n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  27. b10833033 蔡孟頻2020年4月29日 上午9:46

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。

    回覆刪除
  28. B10833034 彭仲麟2020年4月29日 上午9:47

    1.
    遞回函式簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算。


    2.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace T0429
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("算費式數列程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  29. B10833001宋映嫺2020年4月29日 上午9:48

    1.遞回簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算。
    2.↓
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace T0429
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("算費式數列程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  30. B10833096林建鈞2020年4月29日 上午9:48

    1.遞迴就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算
    2.↓
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("算費式數列的程式");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();

    }
    }
    }

    回覆刪除
  31. B10833073毛聖翔2020年4月29日 上午9:51

    說明:費式數列的前兩項為 1、1,之後的每一項為前兩項之和,即 Fn=Fn-1+Fn-2,費式數列的前 10 項為:1、1、2、3、5、8、13、21、34、55。由使用者輸入一個正數數 n ( n < 40 ),計算出費式數列的第 n 項之值並輸出之。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  32. 1.遞回函數簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    2.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("你好");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  33. B10833029 余柏翰2020年4月29日 上午9:54

    所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。

    回覆刪除
  34. B10833046王子榮2020年4月29日 上午9:54

    說明:費式數列的前兩項為 1、1,之後的每一項為前兩項之和,即 Fn=Fn-1+Fn-2,費式數列的前 10 項為:1、1、2、3、5、8、13、21、34、55。由使用者輸入一個正數數 n ( n < 40 ),計算出費式數列的第 n 項之值並輸出之。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  35. B10833025徐郁堂2020年4月29日 上午9:55

    說明:費式數列的前兩項為 1、1,之後的每一項為前兩項之和,即 Fn=Fn-1+Fn-2,費式數列的前 10 項為:1、1、2、3、5、8、13、21、34、55。由使用者輸入一個正數數 n ( n < 40 ),計算出費式數列的第 n 項之值並輸出之。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  36. B10833045施明睿2020年4月29日 上午9:56

    說明:費式數列的前兩項為 1、1,之後的每一項為前兩項之和,即 Fn=Fn-1+Fn-2,費式數列的前 10 項為:1、1、2、3、5、8、13、21、34、55。由使用者輸入一個正數數 n ( n < 40 ),計算出費式數列的第 n 項之值並輸出之。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  37. b10833031游凱程2020年4月29日 上午9:56

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace fibomacci
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  38. B10833004黃薰田2020年4月29日 上午9:56

    說明:費式數列的前兩項為 1、1,之後的每一項為前兩項之和,即 Fn=Fn-1+Fn-2,費式數列的前 10 項為:1、1、2、3、5、8、13、21、34、55。由使用者輸入一個正數數 n ( n < 40 ),計算出費式數列的第 n 項之值並輸出之。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  39. B10833083 夏湘琳2020年4月29日 上午9:57

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp2
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine();
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  40. 1.又譯為遞歸,在數學與電腦科學中,是指在函數的定義中使用函數自身的方法。
    遞迴一詞還較常用於描述以自相似方法重複事物的過程。



    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("費式數列");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  41. b10833037沈泉澔2020年4月29日 上午9:59

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace fibomacci
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  42. b10833037沈泉澔2020年4月29日 上午9:59

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace fibomacci
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  43. b10833037沈泉澔2020年4月29日 上午9:59

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace fibomacci
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  44. 遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace fibomacci
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  45. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  46. D10811007 蘇鈺貽2020年4月29日 上午10:03

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  47. B10833012 何郁麒2020年4月29日 上午10:04

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  48. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  49. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  50. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f({0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  51. 之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  52. 之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  53. 之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  54. 之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  55. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  56. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  57. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  58. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  59. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  60. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _12333
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1) return n;
    else return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n < 40; n++)
    Console.WriteLine("ff({0})={1}", n, ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  61. B10833022 許家銘2020年4月29日 上午10:25

    1. 何謂遞迴函數?
    遞迴函數的定義,就是以自己本身來呼叫自己本身,如果沒有終止條件,程式是跑不完的,所以需要包含終止條件與遞迴條件兩個部份。
    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容。

    2. 實作費氏數列,用遞迴方法
    namespace fib
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費氏數列的程式");
    for (int n=0;n<=40;n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  62. namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if(n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, f(n));
    Console.Read();
    }




    }

    }

    回覆刪除
  63. namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if(n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, f(n));
    Console.Read();
    }




    }

    }

    回覆刪除
  64. D10811007 蘇鈺貽2020年4月29日 上午10:29

    簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。

    回覆刪除
  65. B10833012 何郁麒2020年4月29日 上午10:29

    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。

    回覆刪除
  66. b10833084 詹祐宗2020年4月29日 上午10:30

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace _123
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("費氏數列");
    for (int n = 0; n <= 40; n++)
    Console.WriteLine("f({0})={1}", n, f(n));
    Console.Read();
    }
    }
    }

    在函數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候,必須改變它的參數,例如:
    int R(int n)

    {

    if(結束條件成立) return 預設值;

    return R( n 的運算式 );

    }

    回覆刪除
  67. 所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。 因此函式呼叫其它函式是理所當然的, 但是呼叫自己會不會有些矛盾呢?

    回覆刪除
  68. 所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。 因此函式呼叫其它函式是理所當然的, 但是呼叫自己會不會有些矛盾呢?

    回覆刪除
  69. 所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。 因此函式呼叫其它函式是理所當然的, 但是呼叫自己會不會有些矛盾呢?

    回覆刪除
  70. 所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。 因此函式呼叫其它函式是理所當然的, 但是呼叫自己會不會有些矛盾呢?

    回覆刪除
  71. 所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。 因此函式呼叫其它函式是理所當然的, 但是呼叫自己會不會有些矛盾呢?

    回覆刪除
  72. B10833114 鍾杰廷2020年4月29日 上午10:32

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n ==0 || n== 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("f{0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  73. 所謂的遞迴函式, 簡單地說就是一個呼叫自己的函式。
    每一個 C 程式都是由函式組成的, 由 main() 函式開始執行, main() 函式處理整個問題, 為了降低程式的複雜度, 通常將問題依其特性分解為許多部份, main() 函式呼叫許多獨立的函式來解決個別的問題, 一層一層地分工合作下去。 因此函式呼叫其它函式是理所當然的, 但是呼叫自己會不會有些矛盾呢?

    回覆刪除
  74. B10833114 鍾杰廷2020年4月29日 上午10:34

    什麼是遞回函式(recursion)
    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  75. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp2
    {
    class Program
    {
    static int f(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("f(20)=" + f(20));
    Console
    Console.Read();
    }
    }
    }

    回覆刪除
  76. B10833009 張恩齊2020年4月29日 上午10:53

    什麼是遞回函式(recursion)
    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  77. B10833009 張恩齊2020年4月29日 上午10:53

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if (n ==0 || n== 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0;n <= 50; n++)
    Console.WriteLine("f{0}={1}", n, f(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  78. 什麼是遞回函式(recursion)
    遞回函式(recursive function)簡單來說就是在一個函式當中再去呼叫它自己,其中一個實際的範例就是階層的計算(factorial)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如 5! = 5 x 4 x 3 x 2 x 1。
    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被稱作 後進先出(Last in First Out, LIFO)

    回覆刪除
  79. B10833018 杜立笙2020年4月29日 上午11:08

    數 F(n) 中又呼叫函數 F(n-1) 及 F(n-2),像這樣在函數中又呼叫自己的寫法,就叫做「遞迴 Recursion」,而這種函數就稱為「遞迴函數 Recursive Function」。遞迴函數一開頭要先設定結束條件,否則會無窮循環下去,而遞迴呼叫自己的時候,必須改變它的參數,例如:

    int R(int n)

    {

    if(結束條件成立) return 預設值;

    return R( n 的運算式 );

    }



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);

    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for(int n=0; n<=40; n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }


    回覆刪除
  80. B10833022 許家銘2020年4月29日 上午11:08

    1. 何謂遞迴函數?
    遞迴函數的定義,就是以自己本身來呼叫自己本身,如果沒有終止條件,程式是跑不完的,所以需要包含終止條件與遞迴條件兩個部份。
    之所以能夠透過遞回函式,是因為函式堆疊(stack)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容。

    2. 實作費氏數列,用遞迴方法
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace fib
    {
    class Program
    {
    static int ff(int n)
    {
    if (n == 0 || n == 1)
    return n;
    else
    return ff(n - 1) + ff(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費氏數列的程式");
    for (int n=0;n<=40;n++)
    Console.WriteLine("ff({0})={1}",n,ff(n));
    Console.Read();
    }
    }
    }

    回覆刪除
  81. 遞歸就是一個一個函式當中再去調用它自己,其中一個實際的範例就是階層的計算(階乘)。
    階層聽起來可能很陌生,但是大家高中數學一定接觸過,例如5!= 5 x 4 x 3 x 2 x1。
    之所以能夠透過遞進回傳函式,是因為函式堆疊(堆棧)在執行時有一個特性,當某個函式呼叫另一個函式時,需要等到裡面的函式執行完產生結果後,才會繼續回來執行自己的函式內容,而這樣的情況也被列入後進先出(LIFO後進先出)
    namespace ConsoleApp1
    {
    class Program
    {
    static int f(int n)
    {
    if(n == 0 || n == 1) return n;
    else return f(n - 1) + f(n - 2);
    }
    static void Main(string[] args)
    {
    Console.WriteLine("這是一個算費式數列的程式");
    for (int n = 0; n <= 50; n++)
    Console.WriteLine("ff({0})={1}", n, f(n));
    Console.Read();
    }




    }

    }

    回覆刪除
  82. B10833083 夏湘琳2020年4月29日 上午11:43

    遞迴函數就是在函數的定義裏面呼叫自己的函數。遞迴函數至少需要包含終止條件與遞迴條件兩個 部份。遞迴可以讓程式與數學定義相契合,程式碼較為簡潔易懂。遞迴可能會重複計算相同的東西多次,造成效能的浪費。

    回覆刪除
  83. B10833023 陳姿茹2020年4月29日 上午11:44

    遞迴函數就是在函數的定義裏面呼叫自己的函數。遞迴函數至少需要包含終止條件與遞迴條件兩個 部份。遞迴可以讓程式與數學定義相契合,程式碼較為簡潔易懂。遞迴可能會重複計算相同的東西多次,造成效能的浪費。

    回覆刪除
  84. using System;

    namespace ConsoleApp1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World!");
    }
    static int fib(int n) {
    if (n == 0 || n == 1)
    //終止條件
    return n;
    else
    return fib(n - 1) +
    fib(n - 2);
    }
    static void Main(string[]args)
    {
    Console.WriteLine("f(20)=" + fib(20));
    Console.Read();
    }
    }

    回覆刪除
  85. using System;

    namespace ConsoleApp1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World!");
    }
    static int fib(int n) {
    if (n == 0 || n == 1)
    //終止條件
    return n;
    else
    return fib(n - 1) +
    fib(n - 2);
    }
    static void Main(string[]args)
    {
    Console.WriteLine("f(20)=" + fib(20));
    Console.Read();
    }
    }

    回覆刪除
  86. using System;

    namespace ConsoleApp1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World!");
    }
    static int fib(int n) {
    if (n == 0 || n == 1)
    //終止條件
    return n;
    else
    return fib(n - 1) +
    fib(n - 2);
    }
    static void Main(string[]args)
    {
    Console.WriteLine("f(20)=" + fib(20));
    Console.Read();
    }
    }

    回覆刪除
  87. b10833020 黃俊浩2020年6月17日 上午9:57

    using System;

    namespace ConsoleApp1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World!");
    }
    static int fib(int n) {
    if (n == 0 || n == 1)
    //終止條件
    return n;
    else
    return fib(n - 1) +
    fib(n - 2);
    }
    static void Main(string[]args)
    {
    Console.WriteLine("f(20)=" + fib(20));
    Console.Read();
    }
    }

    回覆刪除

HTML 編輯器