2012-11-05 47 views
3

好的,這裏是我在學習過程中編寫的簡單代碼。99瓶啤酒遞歸似乎不起作用

void SingTheSong (int NumOfBottles) 
{ 
    if (NumOfBottles == 0){ 
     printf("there are simply no more bottles of beer on the wall. \n"); 
    } 

    else { 
     printf("%d bottles of beer on the wall, %d bottles of beer.\n", NumOfBottles, NumOfBottles); 
     int Bottleless = NumOfBottles - 1; 
     printf("Take one down pass it around, %d bottles of beer on the wall. \n", Bottleless); 

     SingTheSong(Bottleless); 
     printf("Put a bottle in the recycling bin, there are now %d empty bottles in the bin.\n", NumOfBottles); 
    } 
} 

int main(int argc, const char * argv[]) 
{ 
    SingTheSong(99); 
    return 0; 
} 

我無法理解的事情就是爲什麼在1 SingTheSong(Botteless)功能啓動時的程序運行,爲什麼它顯示的printf()語句後有啤酒在0瓶壁。只是一個混亂,因爲我認爲在else語句再次運行else語句之前,大括號內的所有內容都在else語句中被引用。爲什麼不是這樣?

例:在牆壁上 「99瓶啤酒,99瓶啤酒以一個向下,圍繞通過它,98瓶的上壁啤酒 將一個瓶的回收箱,現在有1空瓶子在箱子裏「 」98瓶啤酒在牆上,98瓶啤酒。取下一個,繞過它,在牆上的97瓶啤酒 把一瓶放在回收桶裏,現在有2瓶在空箱裏的空瓶子「

我知道他是初學者,我是初學者。有人可以向我解釋這一點,所以我停止在圈子裏。謝謝!

+4

你知道遞歸是如何工作的?從遞歸調用(或任何函數調用)返回時會發生什麼? –

+2

LOL 99遞歸程度 – texasbruce

回答

0

您正在遞交的SingTheSong函數之前您的printf關於回收箱中的瓶子數量。

回收箱邏輯中還存在一個缺陷 - 您需要將回收箱中的瓶子當前數量遞歸地傳遞給您的函數 - 現在的方式,如果您將線條的順序修改爲如上所述,它將總是打印比在垃圾箱中的壁上的瓶子總數少一個。

下面是一些僞代碼,以顯示這種遞歸的應該是什麼樣子:

function singTheSong(bottlesOnWall,bottlesInBin) { 
    if (bottlesOnWall == 0) { 
     print("There are no more bottles.\n"); 
    } else { 
     printf("%d bottles of beer on the wall, %d bottles of beer.\n", bottlesOnWall, bottlesOnWall); 
     bottlesOnWall--; 
     bottlesInBin++; 
     printf("Take one down pass it around, %d bottles of beer on the wall.\n", bottlesOnWall); 
     printf("Put a bottle in the recycling bin, there are now %d empty bottles in the bin.\n", bottlesInBin); 
     singTheSong(bottlesOnWall,bottlesInBin); 
    } 
} 

singTheSong(99,0); 
+0

該程序建立並運行良好。如果在箱子裏打印的數量少於牆上的數量,如果else語句結束並且牆上有0個瓶子,是不是應該在箱子裏有-1個瓶子?或者如果第一個印象是在牆壁上有99個瓶子,那麼它表示箱子裏有98個瓶子。我不知道函數ius如何從99n倒數然後從0上移? – adobbs6017

+0

好吧,我明白你寫的代碼是因爲singTheSong以99和0開頭,但是當它只從100開始遞歸到位時,計算機的思考過程是什麼。 – adobbs6017

0

嗯,我不明白的地方你面臨的問題,但在這裏不用解釋:

對於NumOfBottles = 99:

printf("%d bottles of beer on the wall, %d bottles of beer.\n", NumOfBottles, NumOfBottles); // = 99 bottles of beer on the wall, 99 bottles of beer. 
int Bottleless = NumOfBottles - 1; // 98 
printf("Take one down pass it around, %d bottles of beer on the wall. \n", Bottleless); //98 bottles of beer on the wall. 
SingTheSong(98) 

爲NumOfBottles = 98:

printf("%d bottles of beer on the wall, %d bottles of beer.\n", NumOfBottles, NumOfBottles); //= 98 bottles of beer on the wall, 98 bottles of beer 
int Bottleless = NumOfBottles - 1; // 97 
printf("Take one down pass it around, %d bottles of beer on the wall. \n", Bottleless); //97 bottles of beer on the wall 
SingTheSong(97); 

...

在結束時NumOfBottles = 0: 的printf(「有啤酒在牆壁上根本就沒有更多的瓶子。 。\ n「); //有簡單的牆上啤酒沒有更多的瓶子//現在回到

現在,這將是印刷99次:

printf("Put a bottle in the recycling bin, there are now %d empty bottles in the bin.\n", NumOfBottles);//starting from 1 upto 99. 

這就是爲什麼它不喜歡的!是,因爲遞歸函數調用前是printf("Put a bottle...")

解決方案:

void SingTheSong (int NumOfBottles) 
{ 
    if (NumOfBottles == 0){ 
     printf("there are simply no more bottles of beer on the wall. \n"); 
    } 


    else { 
     printf("%d bottles of beer on the wall, %d bottles of beer.\n", NumOfBottles, NumOfBottles); 
     int Bottleless = NumOfBottles - 1; 
     printf("Take one down pass it around, %d bottles of beer on the wall. \n", Bottleless); 
     printf("Put a bottle in the recycling bin, there are now %d empty bottles in the bin.\n", 99-Bottleless); 
     SingTheSong(Bottleless); 
    } 
} 
1

試想一下,你知道什麼SingTheSong爲方法打印。現在分開交易if聲明的兩個分支。當NumOfBottles爲零時,打印「no bottles」消息並返回。當NumOfBottles不爲零,我們做以下三兩件事:瓶

  • 打印數量N
  • 打印任何SingTheSongN-1
  • 打印回收信息方法打印N

的中間行是遞歸:它可以擴展爲相同的三行,如下所示:

  • 打印瓶N數量
  • 打印瓶N-1數量
  • 打印任何SingTheSongN-2
  • 打印回收消息N-1
  • 打印回收消息方法打印N

你可以d一次又一次,直到中間線變成「脫啤酒」的信息。

0

沒有完整的答案。只是顯示你可能想補充其他答案。

void SingTheSong (int bottlesOnWall, int bottlesInBin) 
{ 
    if (bottlesOnWall == 0){ 
     printf("there are simply no more bottles of beer on the wall. \n"); 
    } 

    else { 
     printf("%d bottles of beer on the wall, %d bottles of beer.\n",bottlesOnWall, bottlesOnWall); 
     printf("Take one down pass it around, %d bottles of beer on the wall. \n", --bottlesOnWall); 
     printf("Put a bottle in the recycling bin, there are now %d empty bottles in the bin.\n", ++bottlesInBin); 

     SingTheSong(bottlesOnWall, bottlesInBin); 
    } 
} 

開始,您可以調用的功能等:

SingTheSong(99, 0); 

此外,你可能想了解更多關於recursion

0

這是一個函數遞歸,當您啓動SingTheSong(無邊距)表單99到1時,它將調用無邊距爲99,98,97 ......的函數...... 1.在SingTheSong(1)之後,然後繼續執行printf \ n「,NumOfBottles);」將一個瓶子放在回收桶中,現在有%d個空瓶子在瓶子裏。\ n「,NumOfBottles);」

然後回到SingTheSong(2),因爲它的推移,終於回來SingTheSong(99)

+0

謝謝你,這是我正在尋找的答案!大聲笑ZND IT是最短的,但感謝任何人。 – adobbs6017

+0

@ adobbs6017您應該考慮標記幫助您解決問題的解決方案(點擊答案左側的綠色複選標記)。 – WhozCraig