鱼C论坛

 找回密码
 立即注册
查看: 1818|回复: 6

[已解决]关于递归问题

[复制链接]
发表于 2021-2-20 14:35:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
def Dec2Bin(dec):
    result = ''
   
    if dec:
        result = Dec2Bin(dec//2)  这个里面result是否有被赋值,在这里result的作用是什么,我发现这个位置的result的值始终是“”空字符
        return result + str(dec%2)
    else:
        return result

print(Dec2Bin(62))
谢谢那位老师能帮我看看。
最佳答案
2021-2-20 20:50:07
本帖最后由 Stubborn 于 2021-2-20 21:06 编辑

你要想明白,递归调用的时候,你一层一层的调用函数(调用的时候是没有返回结果的),等到递归出口的时候,才开始一层一层的往回传结果。    就是说if里面result的复制要等到else return result,返回这个result的时候,才能接受到结果
def Dec2Bin(dec):
    """
    已dec=10举例说明。
    第一次调用:if 10: result = Dec2Bin(5) -->这里继续调用函数,result还没有获得函数的返回结果
                                            下面的return result + str(dec % 2)不会执行
    第二次调用: if 5: result = Dec2Bin(2)
    第三次调用: if 2: result = Dec2Bin(1)
    第四次调用: if 1: result = Dec2Bin(0)
    运行Dec2Bin(0)这个函数的时候,返回result = ''  注意这个是返给第四次调用的
    现在第四次调用收到结果了,开始返回结果:return result + str(1 % 2)-> '' + '1',返给第三次调用的
    第三次调用收到结果:result = "1", 继续返回结果:return result + str(2 % 2)-> '1' + '0',返给第二次调用的
    第二次调用收到结果:result = "10",继续返回结果:return result + str(5 % 2)-> '10' + '1',返给第一次次调用的
    第一次调用收到结果:result = "101",继续返回结果:return result + str(10 % 2)-> '101' + '0'
    最终返回‘1010’
    """
    result = ''

    if dec:
        result = Dec2Bin(dec // 2)
        return result + str(dec % 2)
    else:
        return result
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-2-20 15:09:03 | 显示全部楼层
def Dec2Bin(dec):
    result = ''
   
    if dec:
        print('result = Dec2Bin(%d//2)'%dec)
        result = Dec2Bin(dec//2)
        print(result)
        return result + str(dec%2)
    else:
        return result

print(Dec2Bin(62))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-20 15:53:48 | 显示全部楼层
// 是每次取商
% 每次取余
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-20 16:21:18 | 显示全部楼层

老师我按照你的方法知道了result的值。我现在不明白的地方就是result = dec28in(dec//2)这个地方为什么需要result变量名。然后函数在调用dec28in为什么要赋值给result。而且这个result这个值在return返回值时并不受dec28in(dec//2)赋值的影响始终进行dec%2字符串值的累加。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-20 16:50:06 | 显示全部楼层
print(Dec2Bin(62)) # 这一句,意味着打印函数,变量值为62

第一轮:
   if dec:
        result = Dec2Bin(dec//2)   # 这个位置result的值是,62//2,即:31
        return result + str(dec%2)   # 这个位置 result 为 空置 + 62%2,即:0

第二轮:
  if dec:
        result = Dec2Bin(dec//2)   # 这个位置result的值是,31//2,即:15
        return result + str(dec%2)   # 这个位置 result 为 0 + 31%2,即:10

第三轮:
  if dec:
        result = Dec2Bin(dec//2)   # 这个位置result的值是,15//2,即:7
        return result + str(dec%2)   # 这个位置 result 为 10 + 15%2,即:110

第四轮:
  if dec:
        result = Dec2Bin(dec//2)   # 这个位置result的值是,7//2,即:3
        return result + str(dec%2)   # 这个位置 result 为 110 + 7%2,即:1110

第五轮:
  if dec:
        result = Dec2Bin(dec//2)   # 这个位置result的值是,3//2,即:1
        return result + str(dec%2)   # 这个位置 result 为 1110 + 3%2,即:11110

第五轮:
  if dec:
        result = Dec2Bin(dec//2)   # 这个位置result的值是,1//2,即:0
        return result + str(dec%2)   # 这个位置 result 为 11110 + 1%2,即:111110

第六轮:
  if dec: # 为假,程序跳过此步骤

评分

参与人数 1荣誉 +5 鱼币 +1 贡献 +3 收起 理由
redaiconglin + 5 + 1 + 3 就是上面调用函数的result和下面返回的resu.

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-20 20:50:07 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Stubborn 于 2021-2-20 21:06 编辑

你要想明白,递归调用的时候,你一层一层的调用函数(调用的时候是没有返回结果的),等到递归出口的时候,才开始一层一层的往回传结果。    就是说if里面result的复制要等到else return result,返回这个result的时候,才能接受到结果
def Dec2Bin(dec):
    """
    已dec=10举例说明。
    第一次调用:if 10: result = Dec2Bin(5) -->这里继续调用函数,result还没有获得函数的返回结果
                                            下面的return result + str(dec % 2)不会执行
    第二次调用: if 5: result = Dec2Bin(2)
    第三次调用: if 2: result = Dec2Bin(1)
    第四次调用: if 1: result = Dec2Bin(0)
    运行Dec2Bin(0)这个函数的时候,返回result = ''  注意这个是返给第四次调用的
    现在第四次调用收到结果了,开始返回结果:return result + str(1 % 2)-> '' + '1',返给第三次调用的
    第三次调用收到结果:result = "1", 继续返回结果:return result + str(2 % 2)-> '1' + '0',返给第二次调用的
    第二次调用收到结果:result = "10",继续返回结果:return result + str(5 % 2)-> '10' + '1',返给第一次次调用的
    第一次调用收到结果:result = "101",继续返回结果:return result + str(10 % 2)-> '101' + '0'
    最终返回‘1010’
    """
    result = ''

    if dec:
        result = Dec2Bin(dec // 2)
        return result + str(dec % 2)
    else:
        return result

评分

参与人数 1荣誉 +5 鱼币 +1 贡献 +3 收起 理由
redaiconglin + 5 + 1 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-20 21:10:37 | 显示全部楼层

看看6#把,递归,有一个递的过程和一个归的过程
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-28 18:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表