3

好的,这是我在学习过程中编写的简单代码。

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 开始,为什么在墙上有 0 瓶啤酒后它显示 printf() 语句。只是有点困惑,因为我认为花括号中的所有内容都在 else 语句中被执行,然后再运行 else 语句。为什么不是这样?

示例:“墙上有 99 瓶啤酒,99 瓶啤酒。拿下一个,传过来,墙上有 98 瓶啤酒。将一瓶放在回收箱中,现在垃圾桶里有 1 个空瓶” “墙上98瓶啤酒,98瓶啤酒。拿下来,传过来,墙上97瓶啤酒。把一瓶放在回收箱里,现在垃圾桶里有2个空瓶子”

我知道他是初学者的东西,我是初学者。有人可以向我解释一下,所以我不再绕圈子了。谢谢!

4

5 回答 5

1

想象一下,您知道SingTheSong打印什么方法Nif现在分别交易语句的两个分支。当NumOfBottles为零时,打印“没有瓶子”消息,然后返回。当NumOfBottles不为零时,我们做以下三件事:

  • 打印瓶数N
  • 打印任何SingTheSong打印的方法N-1
  • 打印回收信息N

中间行是递归的:它可以扩展为相同的三行,如下所示:

  • 打印瓶数N
  • 打印瓶数N-1
  • 打印任何SingTheSong打印的方法N-2
  • 打印回收信息N-1
  • 打印回收信息N

您可以一次又一次地这样做,直到中间线变成“啤酒用完”消息。

于 2012-11-05T01:29:37.933 回答
0

这是一个函数递归,当你启动 SingTheSong(Bottleless) 从 99 到 1 时,它会调用 Bottleless 为 99,98,97……1 的函数。在 SingTheSong(1) 之后,然后继续 printf

" printf("把瓶子放到回收箱里,现在回收箱里有 %d 个空瓶子。\n", NumOfBottles);"

然后回到 SingTheSong(2),随着它继续,终于回到 SingTheSong(99)

于 2012-11-05T01:49:40.920 回答
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);
于 2012-11-05T01:26:02.230 回答
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);
     }
}
于 2012-11-05T01:28:45.207 回答
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);

此外,您可能想了解更多关于递归的信息。

于 2012-11-05T01:31:49.290 回答