可能是一個非常初學者的問題,但它令人難以置信。
我有一個斐波那契數列的例子。
fib(int n) {
if (n <= 1) { //Base Case
return n;
}
return fib(n - 1) fib(n - 2)
}
所以我主要在理解函式如何迭代方面有問題,當我一步一步列印每個迭代時,它仍然沒有意義。
演算法本身是有效的,但是序列如何隨著時間的推移變得更小,從而最終滿足基本情況的條件?
例如,如果我們傳入 n=6,第一個數字應該是 9,下一步 n=11,然后它就會變大。但是當我列印它時,演算法從 6-0 開始倒計時,然后我得到 0 到 2 之間的亂數,直到它給我正確的數字。
uj5u.com熱心網友回復:
當你傳入 n=6 時,你的函式被呼叫兩次,n=5 和 n=4
對于 n=5,在 n=4 和 n=3 時呼叫兩次
對于 n=4,在 n=3 和 n=2 時呼叫兩次,
...等等
最終,所有呼叫都變為 n=1 或 n=0,其中您的第一個 if 陳述句停止遞回。
uj5u.com熱心網友回復:
n永遠不會改變。它不會變小。相反,每個呼叫都有自己的n.
讓我們看一個更簡單但非常相似的例子。
int fact(int n) {
if (n <= 1) {
return 1;
}
return n * fact(n-1);
}
fact(1)是1。那很容易。fact(2)是2 * fact(1)。從上面,我們知道fact(1)是1,fact(2)也是2 * 1,這是2。fact(3)是3 * fact(2),這是3 * 2,這是6。fact(4)是4 * fact(3),這是4 * 6,這是24。fact(5)是5 * fact(4),這是5 * 24,這是120。- 等等。
斐波那契非常相似。
fib(0)是1fib(1)是1fib(2)是fib(1) fib(0),這是1 1,這是2fib(3)是fib(2) fib(1),這是2 1,這是3fib(4)是fib(3) fib(2),這是3 2,這是5fib(5)是fib(4) fib(3),這是5 3,這是8- 等等
這比上面暗示的要多得多。
fib(5)
= fib(4) fib(3)
= fib(3) fib(2) fib(2) fib(1)
= fib(2) fib(1) fib(1) fib(0) fib(1) fib(0) 1
= fib(1) fib(0) 1 1 1 1 1 1
= 1 1 1 1 1 1 1 1
= 8
總共有 15 次呼叫fibfor fib(5)!
fib(0): 1 個電話。fib(1): 1 個電話。fib(2): 3個電話。fib(3): 5 個電話。fib(4): 9 個電話。fib(5): 15 個電話。- 等等
(這個數列與斐波那契數列非常相似!)
uj5u.com熱心網友回復:
遞回函式的基本特征是它呼叫自身。遞回函式不會像您所理解的那樣進行迭代。相反,它會反復出現,因為它一次又一次地發生。
在您的fib函式中,呼叫自身的部分在回傳中。
return fib(n - 1) fib(n - 2)
在這種情況下,它實際上呼叫了自己兩次。首先,輸入變數減 1 fib(n - 1),第二,輸入變數減 2 fib(n - 2),它不斷呼叫自己,直到n小于或等于 1。一旦達到該條件,函式開始回傳數字。首先,它回傳 0 和 1,但之后它開始回傳相加的數字。執行步驟顯示在@cptFracassa 的回答中。
有什么需要注意的;如果遞回函式沒有基本/退出案例,那么它只會不斷呼叫自己,直到您終止行程或機器發生壞事為止。在您的情況下,基本情況是if最終停止呼叫自身并僅回傳一個數字。
uj5u.com熱心網友回復:
您宣告您已列印所有值以了解發生了什么。
我懷疑您只列印了值,而沒有說明為什么會出現該值。
這是一個修改后的代碼版本,它告訴您更詳細的值故事。
請注意,該程式非常健忘,一旦計算和使用就無法記住值。如果再次需要相同的值,它會高興地再次計算它。
#include <stdio.h>
fib(int n){
int retValue=0;
printf("Trying to determine fibonacci at index %d.", n);
if(n<=1){ //Base Case
printf(" Easy, it is %d.\n", n);
return n;
}
printf(" No idea. But I could sum up the fibonaccis at index %d and index %d.\n", n-1, n-2);
retValue= fib(n-1) fib(n-2);
printf("I now know the fibonaccis at index %d and at index %d, their sum is %d.\n", n-1, n-2, retValue);
return retValue;
}
int main()
{
const int TryWith = 6;
printf ("The fibonacci at index %d is %d.\n", TryWith, fib(TryWith));
return 0;
}
輸出是:
Trying to determine fibonacci at index 6. No idea. But I could sum up the fibonaccis at index 5 and index 4.
Trying to determine fibonacci at index 5. No idea. But I could sum up the fibonaccis at index 4 and index 3.
Trying to determine fibonacci at index 4. No idea. But I could sum up the fibonaccis at index 3 and index 2.
Trying to determine fibonacci at index 3. No idea. But I could sum up the fibonaccis at index 2 and index 1.
Trying to determine fibonacci at index 2. No idea. But I could sum up the fibonaccis at index 1 and index 0.
Trying to determine fibonacci at index 1. Easy, it is 1.
Trying to determine fibonacci at index 0. Easy, it is 0.
I now know the fibonaccis at index 1 and at index 0, their sum is 1.
Trying to determine fibonacci at index 1. Easy, it is 1.
I now know the fibonaccis at index 2 and at index 1, their sum is 2.
Trying to determine fibonacci at index 2. No idea. But I could sum up the fibonaccis at index 1 and index 0.
Trying to determine fibonacci at index 1. Easy, it is 1.
Trying to determine fibonacci at index 0. Easy, it is 0.
I now know the fibonaccis at index 1 and at index 0, their sum is 1.
I now know the fibonaccis at index 3 and at index 2, their sum is 3.
Trying to determine fibonacci at index 3. No idea. But I could sum up the fibonaccis at index 2 and index 1.
Trying to determine fibonacci at index 2. No idea. But I could sum up the fibonaccis at index 1 and index 0.
Trying to determine fibonacci at index 1. Easy, it is 1.
Trying to determine fibonacci at index 0. Easy, it is 0.
I now know the fibonaccis at index 1 and at index 0, their sum is 1.
Trying to determine fibonacci at index 1. Easy, it is 1.
I now know the fibonaccis at index 2 and at index 1, their sum is 2.
I now know the fibonaccis at index 4 and at index 3, their sum is 5.
Trying to determine fibonacci at index 4. No idea. But I could sum up the fibonaccis at index 3 and index 2.
Trying to determine fibonacci at index 3. No idea. But I could sum up the fibonaccis at index 2 and index 1.
Trying to determine fibonacci at index 2. No idea. But I could sum up the fibonaccis at index 1 and index 0.
Trying to determine fibonacci at index 1. Easy, it is 1.
Trying to determine fibonacci at index 0. Easy, it is 0.
I now know the fibonaccis at index 1 and at index 0, their sum is 1.
Trying to determine fibonacci at index 1. Easy, it is 1.
I now know the fibonaccis at index 2 and at index 1, their sum is 2.
Trying to determine fibonacci at index 2. No idea. But I could sum up the fibonaccis at index 1 and index 0.
Trying to determine fibonacci at index 1. Easy, it is 1.
Trying to determine fibonacci at index 0. Easy, it is 0.
I now know the fibonaccis at index 1 and at index 0, their sum is 1.
I now know the fibonaccis at index 3 and at index 2, their sum is 3.
I now know the fibonaccis at index 5 and at index 4, their sum is 8.
The fibonacci at index 6 is 8.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331908.html
上一篇:Cs默認int型別和<stdint.h>的intXX_t型別之間的轉換如何作業?
下一篇:C中的遞回函式列印1到n到1
