【问题标题】:Recursive factorial return statement递归阶乘返回语句
【发布时间】:2019-10-07 14:30:44
【问题描述】:

为什么我们使用return 1 来终止递归函数?是否可以将任何其他值用作默认值,例如 1。

如果我们返回1作为函数的返回值,那么为什么1没有返回给主函数。

 #include<stdio.h>
 int fact(int n)
 {
   if(n>=1)
      return (n*fact(n-1));
   else
      return 1;
 }
 int main()
 {
   int a,ans;
   scanf("%d",&a);
   ans=fact(a);
   printf("factorial of %d is %d ",a,ans);
   return 0;
  }
  /*
   explanation
          fact(4);
          if(4>=1) 4*fact(3)
          if(3>=1) 4*3*fact(2)
          if(2>=1) 4*3*2*fact(1)
          if(1>=1) 4*3*2*1*fact(0)
          if(0>=1) return 1;

  */

【问题讨论】:

  • 因为 0!是 1。
  • 您的评论就是答案。你打电话给fact(4),而不是fact(0)
  • 请仅标记您使用的语言。你的问题比较不清楚。试试fact(0),用调试器看看1确实返回到main

标签: c recursion return-value factorial


【解决方案1】:

n==0时执行return语句。

n==0 的阶乘是 1,所以我们返回 1。

【讨论】:

    【解决方案2】:

    为什么我们使用“return 1”来终止递归函数

    因为这应该涵盖n 不是&gt;=1 的情况,换句话说,当n0 时。我不认为否定n 是有效的。 0!1,因此它返回该值。

    如果我们返回 1 作为函数结束,那么为什么不返回 1 主要功能。

    如果函数使用01 调用n,则1 将返回到主函数。对于任何其他值,1 仅在递归阶乘调用中返回,返回给main 函数的值不是1,而是(n*fact(n-1)),在这些情况下不是1 .

    【讨论】:

    • then why 1 is not returned to main function... 我认为这与您在上一段中解释的有点不同。
    • 好收获。我会解决的。
    【解决方案3】:
       /*
          explanation
              fact(4);
              if(4>=1) 4*fact(3)
              if(3>=1) 4*3*fact(2)
              if(2>=1) 4*3*2*fact(1)
              if(1>=1) 4*3*2*1*fact(0)
              if(0>=1) return 1; now return is default tend to multiply as we give 1 and 
               return has already 24 in its stack so 1*24 is returned to main()
              if we give return 2; 2*24 is returned to main();
    
        */
    

    我们不希望我们的最终结果受到影响,为了解决这个错误,任何乘以 1 的结果都相同,所以我们在递归函数中使用 1 作为返回。

    实际上return也是一个堆栈寄存器,在函数中调用with时保存临时变量,它默认通过multiply属性操作,因为我们总是只能发送一个返回值。

    【讨论】:

      【解决方案4】:
      int fact(int n)
      {
          if (n >= 1)
              return n * fact(n-1);
          else
              return 1;
      }
      

      每次调用fact() 函数时,它都会运行return n * fact(n-1); 语句或return 1; 语句,但不能同时运行。

      您在main() 中致电fact(4)。它是这样运行的:

      main:
        compute fact(4)
        fact(4):
        |  4 >= 1?  Yes!
        |  compute 4 * fact(3)
        |    fact(3):
        |    |  3 >= 1?  Yes!
        |    |  compute 3 * fact(2)
        |    |    fact(2):
        |    |    |  2 >= 1? Yes!
        |    |    |  compute 2 * fact(1)
        |    |    |    fact(1):
        |    |    |    |  1 >= 1? Yes!
        |    |    |    |  compute 1 * fact(0)
        |    |    |    |    fact(0):
        |    |    |    |    |  0 >= 1? NO!
        |    |    |    |    |  return 1;
        |    |    |    |    +--> 1
        |    |    |    |  fact(0) is 1, return 1 * 1 (--> 1)
        |    |    |    +--> 1
        |    |    |  fact(1) is 1, return 2 * 1 (--> 2)
        |    |    +--> 2
        |    |  fact(2) is 2, return 3 * 2 (--> 6)
        |    +--> 6
        |  fact(5) is 6, return 4 * 6 (--> 24)
        +--> 24
        fact(4) is 24, assign it to `ans`, print it etc
      // end of main
      

      当一个函数使用return 语句时(或者在它执行最后一条语句后,如果没有达到return,控制权会被传递回调用它的表达式。

      【讨论】:

        猜你喜欢
        • 2018-10-16
        • 2015-12-22
        • 2023-03-30
        • 2023-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 2016-01-28
        • 1970-01-01
        相关资源
        最近更新 更多