2014-12-05 124 views
0

我很難弄清楚如何使用遞歸來使數字加倍,問題是函數必須是靈活的,它必須能夠被調用來加倍數字,然後加倍結果再次繼續,直到停止。例如,每當你戳到一個粘液時,我就需要用遞歸來表示這個(1-> 2-> 4-> 8-> 16),然而當我努力的時候我已經撞到了牆上很好地理解遞歸。在C++中使用遞歸來重複一個數字

我開始喜歡的東西

int doublesomething(int N, int X){ 
return N * X; 
} 

然後把這個到另一個函數,但我一無所獲搞清楚如何給它沒有功能的循環加倍和重置數字,我會鏈接代碼我這裏只有老老實實只是胡言亂語,因爲我已經那種自己失去了下來遞歸

int doublethis(int times){ 
if (times == 0){ 
    return 0; 
    } else { 
    int number; 
    doublesomething(2 , number); 
    int doubled = doublethis(times - 1); 

    } 
    return doubled; 
} 

的兔子洞,這是據我已經得到了,我認爲這是極其錯誤的。

+0

你是否需要''recursion?至於功課還是什麼的? – Jcl 2014-12-05 05:59:19

+0

是的,它必須具有一個函數,其中有一個參數可以加倍,並返回剩下的數量。我試圖避免發佈實際的作業,所以我學到了一些東西,但我無法弄清楚如何使用遞歸來加倍數字。 我在考慮讓一個函數在第二個函數中加倍和調用它。 – defury 2014-12-05 06:03:36

+0

你不能用單個參數遞減(除非你將中間結果存儲在函數外的某個變量中),你需要存儲中間數字並傳遞它,並且你需要一個停止條件(這應該是當'你的函數中的times參數達到1) – Jcl 2014-12-05 06:05:11

回答

0

你需要某種形式的終止條件,例如:

int f(int n, int x) 
{ 
    if (n == 1) 
     return x; 

    return n * x + f(n - 1, x); 
} 
+0

如果我正確地遵循它,這將返回'n!',因爲您乘以時間,而不是2 – Jcl 2014-12-05 06:28:42

+0

(實際上,'x!') – Jcl 2014-12-05 06:35:33

+1

我正在說明遞歸如何工作,你試圖給你遞歸的東西。正如你所說,它不會加倍,所以我將它重命名爲「f」。 – 2014-12-05 06:45:29

0

你並不需要爲這個遞歸,但如果你需要它由於某種原因(作業,學習),根據您的代碼:

int doublethis(int times, int number) { 
    if (times <= 0) { 
    return 0; 
    } else { 
    number *= 2; 
    if((--times) > 0) 
     number = doublethis(times, number); 
    return number; 
    } 
} 

如果你是積極的times將永遠是第一次調用1個或更多的簡化版本:

int doublethis(int times, int number) { 
    if (times == 1) { 
    return number; 
    } else {   
    return doublethis(times-1, number * 2); 
    } 
} 

還是更短:

int doublethis(int times, int number) { 
    return (times == 1) ? number : doublethis(times-1, number * 2); 
} 

這將返回相同的號碼,如果times爲1 ...如果你想讓它實際上一倍,如果調用times = 1那麼times==1狀態更改爲times==0(會更有感)

+0

如果返回'number'作爲終止條件,則不需要'if(( - times)> 0)'。 – 2014-12-05 06:09:56

+0

@ KenY-N我期待在第一次通話中'times'可能是0的情況,但是,如果你肯定不會,它可以被大大簡化。試圖遵循OP的代碼並相應地進行修改。 – Jcl 2014-12-05 06:11:17

+0

我的代碼更浪費了幾個小時的時間,所以如果有一種簡單的方法來修改你所做的事情,那基本上就是我正在尋找的東西。 – defury 2014-12-05 06:13:31

0

你可以試試這個...

#include <iostream> 
using namespace std; 

void doublethis(int n,int time) 
{ 
    if(time==0) 
    return; 
    cout<<" -> " <<n*2; 
    doublethis(n*2,time-1); 
    return; 
} 

int main() 
{ 
    int n,time; 
    cin>>n>>time; 
    cout<<n ; 
    doublethis(n,time); 
    return 0; 
} 

,如果你要仔細3點的四倍,這段代碼的輸出將是

3 -> 6 -> 12 -> 24 -> 48 
1

我不知道這是你打算解決這個問題,你可以參考一下。

int doublethis(int times){ 
    int result; 
    if (times == 0){ 
     result = 1; 
    } 
    if(times > 0) { 
     result = 2 * doublethis(times - 1); 
    } 
    printf("%d \n", result); 
    return result; 
} 

這是遞歸的一個簡單的例子,我想你可以逐步學習算法,並使用圖進行算法的過程。如果你輸入的5倍,行爲是這樣的:

result5 
= 2 * doublethis(4) 
= 2 * 2 * doublethis(3) 
= 2 * 2 * 2 * doublethis(2) 
= 2 * 2 * 2 * 2 * doublethis(1) 
= 2 * 2 * 2 * 2 * 2 * doublethis(0) 
print result0 
print result1 
print result2 
print result3 
print result4 
print result5 
0
#include <iostream> 

using namespace std; 

void doubleThis(int num, int until) { 
    if (until == 0) { 
     return; 
    } 
    num = num * 2; 
    cout << num << endl; 
    doubleThis(num, until-1); 
} 


int main() { 
    int x; 
    cout << "Enter the the number to be doubled for next 5 times " << endl; 
    cin >> x; 
    cout << "Entered number is " << x << endl; 
    doubleThis(x, 5); 

    return 0; 
} 

它完成的序列一倍沒有爲5位的數字。您可以根據需要修改任意數量的代碼。

+0

對不起,如果這個評論不合適,但我討厭在SO人中如何傾向於承擔OP的需求(沒有他明確地添加它們)。你怎麼知道這是一個控制檯應用程序?你怎麼知道結果需要輸出(而不是返回,這很可能會改變你的例子)?爲什麼你會認爲他需要通過'cin'輸入的號碼?不要試圖在這裏摧毀你的答案,但我已經看到了很多次,所以對我來說只是感覺不對(發生在對這個同樣問題的其他答案中)。 – Jcl 2014-12-05 06:54:56