99瓶啤酒递归的似乎并不工作 [英] 99 Bottle of Beer Recursion Doesn't Seem to Work
问题描述
确定这里是一个简单的code我已经同时在一个学习的过程写的。
Ok here is the simple code i have written while on a learning process.
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;
}
我无法理解的唯一事情就是为什么用SingTheSong(Botteless)函数开始在1当程序运行时,为什么它显示的printf()语句后有啤酒在墙上0瓶。因为我认为花括号内的一切困惑只是liuttle在else语句exicuted再次运行else语句之前。为什么是不是这样呢?
the only thing i cant understand is why does the SingTheSong(Botteless) function start at 1 when the program is run, and why does it show the printf() statement after there is 0 bottles of beer on the wall. Just a liuttle confused because i thought everything within the curly braces was exicuted in the else statement before it runs the else statement again. why isn't it like this?
例如:
在墙上,99瓶啤酒99瓶啤酒。取一跌,各地通过它,啤酒在墙上的98瓶。
放一瓶在回收箱,有在bin现在1空瓶子
啤酒在墙上,啤酒瓶98瓶98。取一跌,各地通过它,啤酒在墙上的97瓶。
放一瓶在回收箱,有在bin现在2个空瓶
Example: "99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around, 98 bottles of beer on the wall. Put a bottle in the recycling bin, there is now 1 empty bottles in the bin" "98 bottles of beer on the wall, 98 bottles of beer. Take one down, pass it around, 97 bottles of beer on the wall. Put a bottle in the recycling bin, there is now 2 empty bottles in the bin"
我知道他是初学者的东西,我是一个初学者。有人可以给我讲解一下,所以我停止兜兜。谢谢!
i know his is beginner stuff, i am a beginner. can someone explain this to me so i stop going in circles. thanks!
推荐答案
想象一下,你知道是什么 SingTheSong
为 N $ C方法打印$ C>。现在换
的两个分支,如果
语句分开。当 NumOfBottles
为零,打印出无瓶的消息,并返回。当 NumOfBottles
不为零,我们做以下三件事情:
Imagine that you know what SingTheSong
method prints for N
. Now trade the two branches of the if
statement separately. When NumOfBottles
is zero, print the "no bottles" message, and return. When NumOfBottles
is not zero, we do the following three things:
- 打印瓶数
N
- 打印任何
SingTheSong
方法版画N-1
- 打印回收信息
N
- Print the number of bottles
N
- Print whatever
SingTheSong
method prints forN-1
- Print the recycling message
N
中间线的递归的:它可以扩展到同三线,像这样的:
The middle line is recursive: it can be expanded into the same three lines, like this:
- 打印瓶数
N
- 打印瓶数
N-1
- 打印任何
SingTheSong
方法版画N-2
- 打印回收信息
N-1
- 打印回收信息
N
- Print the number of bottles
N
- Print the number of bottles
N-1
- Print whatever
SingTheSong
method prints forN-2
- Print the recycling message
N-1
- Print the recycling message
N
您可以反复做,直到中间线将成为出来的啤酒的字样。
You can do it again and again, until the middle line becomes the "out of beer" message.
这篇关于99瓶啤酒递归的似乎并不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!