1 / 5

97-2 Programming Language

97-2 Programming Language. Exercise #2: Recursive function. Demo 1. 試撰寫 int fib(int n) 函數,以遞迴函數來求 費氏數列 ,並找出 fib(6)=? 。. Demo 2. 試以遞迴的方式撰寫函數 int sum(int n) , 並利用遞迴公式 sum(n) = n + sum(n - 1) , sum(1) = 1 用來計算 1+2+3+...+n 的值。. Exercise.

gabby
Download Presentation

97-2 Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 97-2 Programming Language Exercise #2: Recursivefunction

  2. Demo 1 • 試撰寫int fib(int n) 函數,以遞迴函數來求費氏數列,並找出fib(6)=?。

  3. Demo 2 • 試以遞迴的方式撰寫函數int sum(int n),並利用遞迴公式sum(n) = n + sum(n - 1),sum(1) = 1用來計算1+2+3+...+n的值。

  4. Exercise • 試利用公式sum(n) = sum(n - 1) + 2*n,sum(1) = 2,撰寫遞迴函數int sum(int n),計算2+4+6+...+2n之和。 • Input the number n : 52 + 4 + 6 + ... + 2n = 30

  5. 費氏數列 • 費氏數列的規則如下所示: • 費氏數列的數學表示式: 0 if n = 0 Fib(n) = 1 if n = 1 fib(n – 1) + fib(n – 2) if n>=2 back

More Related