繁体   English   中英

两个相似的递归代码

[英]Two similar recursion codes

我正在准备面试,但递归让我感到困惑。 我对两个相似代码的 o/p 感到困惑

def fun(x):
   if(x > 0):
        x -= 1
        fun(x) 
        print(x , end=" ")
        x -= 1
        fun(x) 
   
# Driver code
a = 4
fun(a) 

output=0 1 2 0 3 0 1

我很困惑,为什么在上面的代码 4 中,中间值没有像下面的代码一样被推送到堆栈中。

def fun(x):
   if(x > 0):
        fun(x-1) 
        print(x , end=" ")
        x -= 1
        fun(x) 
   
# Driver code
a = 4
fun(a) 

output=1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

调试

我修改了您的代码以显示确切的执行路径,以便您了解 output 发生的原因。

第一个代码的修改版本:

def fun(x, depth=0):
   print(" " * depth + f"in fun({x}) (depth {depth})")
   print(" " * depth + f"if({x} > 0): (depth {depth})")
   if(x > 0):
        print(" " * depth + f"x = {x} - 1 = {x - 1} (depth {depth})")
        x -= 1
        print(" " * depth + f"calling fun({x}) (depth {depth})")
        fun(x, depth + 1)
        print(" " * depth + f"\"{x} \" printed (depth {depth})************")
        print(" " * depth + f"x = {x} - 1 = {x - 1} (depth {depth})")
        x -= 1
        print(" " * depth + f"calling fun({x}) (depth {depth})")
        fun(x, depth + 1)
   
# Driver code
a = 4
fun(a)

Output:

in fun(4) (depth 0)
if(4 > 0): (depth 0)
x = 4 - 1 = 3 (depth 0)
calling fun(3) (depth 0)
 in fun(3) (depth 1)
 if(3 > 0): (depth 1)
 x = 3 - 1 = 2 (depth 1)
 calling fun(2) (depth 1)
  in fun(2) (depth 2)
  if(2 > 0): (depth 2)
  x = 2 - 1 = 1 (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "0 " printed (depth 3)************
   x = 0 - 1 = -1 (depth 3)
   calling fun(-1) (depth 3)
    in fun(-1) (depth 4)
    if(-1 > 0): (depth 4)
  "1 " printed (depth 2)************
  x = 1 - 1 = 0 (depth 2)
  calling fun(0) (depth 2)
   in fun(0) (depth 3)
   if(0 > 0): (depth 3)
 "2 " printed (depth 1)************
 x = 2 - 1 = 1 (depth 1)
 calling fun(1) (depth 1)
  in fun(1) (depth 2)
  if(1 > 0): (depth 2)
  x = 1 - 1 = 0 (depth 2)
  calling fun(0) (depth 2)
in fun(0) (depth 3)
   if(0 > 0): (depth 3)
  "0 " printed (depth 2)************
  x = 0 - 1 = -1 (depth 2)
  calling fun(-1) (depth 2)
   in fun(-1) (depth 3)
   if(-1 > 0): (depth 3)
"3 " printed (depth 0)************
x = 3 - 1 = 2 (depth 0)
calling fun(2) (depth 0)
 in fun(2) (depth 1)
 if(2 > 0): (depth 1)
 x = 2 - 1 = 1 (depth 1)
 calling fun(1) (depth 1)
  in fun(1) (depth 2)
  if(1 > 0): (depth 2)
  x = 1 - 1 = 0 (depth 2)
  calling fun(0) (depth 2)
   in fun(0) (depth 3)
   if(0 > 0): (depth 3)
  "0 " printed (depth 2)************
  x = 0 - 1 = -1 (depth 2)
  calling fun(-1) (depth 2)
   in fun(-1) (depth 3)
   if(-1 > 0): (depth 3)
 "1 " printed (depth 1)************
 x = 1 - 1 = 0 (depth 1)
 calling fun(0) (depth 1)
  in fun(0) (depth 2)
  if(0 > 0): (depth 2)

第二个代码的修改版本:

def fun(x, depth=0):
   print(" " * depth + f"in fun({x}) (depth {depth})")
   print(" " * depth + f"if({x} > 0): (depth {depth})")
   if(x > 0):
        print(" " * depth + f"calling fun({x-1}) (depth {depth})")
        fun(x-1, depth + 1)
        print(" " * depth + f"\"{x} \" printed (depth {depth})************")
        print(" " * depth + f"x = {x} - 1 = {x - 1} (depth {depth})")
        x -= 1
        print(" " * depth + f"calling fun({x}) (depth {depth})")
        fun(x, depth + 1)
   
# Driver code
a = 4
fun(a) 

Output:

in fun(4) (depth 0)
if(4 > 0): (depth 0)
calling fun(3) (depth 0)
 in fun(3) (depth 1)
 if(3 > 0): (depth 1)
 calling fun(2) (depth 1)
in fun(2) (depth 2)
  if(2 > 0): (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
  "2 " printed (depth 2)************
  x = 2 - 1 = 1 (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
 "3 " printed (depth 1)************
 x = 3 - 1 = 2 (depth 1)
 calling fun(2) (depth 1)
  in fun(2) (depth 2)
  if(2 > 0): (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
  "2 " printed (depth 2)************
  x = 2 - 1 = 1 (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
"4 " printed (depth 0)************
x = 4 - 1 = 3 (depth 0)
calling fun(3) (depth 0)
 in fun(3) (depth 1)
 if(3 > 0): (depth 1)
 calling fun(2) (depth 1)
  in fun(2) (depth 2)
  if(2 > 0): (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
  "2 " printed (depth 2)************
  x = 2 - 1 = 1 (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
 "3 " printed (depth 1)************
 x = 3 - 1 = 2 (depth 1)
 calling fun(2) (depth 1)
  in fun(2) (depth 2)
  if(2 > 0): (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
  "2 " printed (depth 2)************
  x = 2 - 1 = 1 (depth 2)
  calling fun(1) (depth 2)
   in fun(1) (depth 3)
   if(1 > 0): (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)
   "1 " printed (depth 3)************
   x = 1 - 1 = 0 (depth 3)
   calling fun(0) (depth 3)
    in fun(0) (depth 4)
    if(0 > 0): (depth 4)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM