好的,這裏是我在學習過程中編寫的簡單代碼。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瓶在空箱裏的空瓶子「
我知道他是初學者,我是初學者。有人可以向我解釋這一點,所以我停止在圈子裏。謝謝!
你知道遞歸是如何工作的?從遞歸調用(或任何函數調用)返回時會發生什麼? –
LOL 99遞歸程度 – texasbruce