鱼C论坛

 找回密码
 立即注册
查看: 1811|回复: 3

[已解决]使用递归编写一个十进制转换为二进制的函数

[复制链接]
发表于 2020-2-28 22:42:24 | 显示全部楼层 |阅读模式

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

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

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

print(Dec2Bin(62))



代码看不懂  请大神帮忙注释一下  真心的谢谢
最佳答案
2020-2-28 23:12:05
  1. def Dec2Bin(dec):
  2.     result = ''
  3.    
  4.     if dec: #如果dec不等于0
  5.         result = Dec2Bin(dec//2)
  6.         return result + str(dec%2)
  7. '''
  8.                result + str(dec%2)=Dec2Bin(62//2) + str(62%2)  
  9.                                   =Dec2Bin(31) + str(62%2)
  10.                                   =Dec2Bin(31//2) +str(31%2) + str(62%2)
  11.                                   =Dec2Bin(15) +str(31%2) + str(62%2)
  12.                                   =Dec2Bin(15//2) + str(15%2) +str(31%2) + str(62%2)
  13.                                   =Dec2Bin(7) + str(15%2) +str(31%2) + str(62%2)
  14.                                   =Dec2Bin(7//2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  15.                                   =Dec2Bin(3) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  16.                                   =Dec2Bin(3//2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  17.                                   =Dec2Bin(1) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  18.                                   =Dec2Bin(1//2)+ str(1%2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  19.                                   =Dec2Bin(0)+ str(1%2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  20.                                   ='' +'1'+'1'+'1'+'1'+'1'+'0'
  21.                                   ='111110'
  22. '''
  23.     else:
  24.         return result

  25. print(Dec2Bin(62))
  26.         
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-28 22:50:16 | 显示全部楼层
    为了讨论方便,先简化程序代码
  1. def Dec2Bin(dec):
  2.     result = ''
  3.     if dec:
  4.         result = Dec2Bin(dec // 2) + str(dec % 2)
  5.     return result
复制代码

      递归就是函数自己调用自己的行为,递归函数一般都有两个分支,一个有递归,另一个无递归,一开始都有递归,当执行到无递归分支的时候,递归就到底了。以底为界,递归分为进入过程和退出过程。就本例而言,递归的进入过程到 Dec2Bin(0) 也就是 dec = 0 的候到底,并开始有了确切的返回值,不过,这个返回值只是一个空字符串 '',然后,开始了逐级退出的过程,并依次确定了各自的返回值。最后返回字符串 '111110',其实就是从递归到底开始,把各级的递归中的 dec % 2 转化成字符串然后按顺序加在一起得到。
  1. Dec2Bin(62) = Dec2Bin(31) + '0'                                  # Dec2Bin(31) + '0' = Dec2Bin(62)
  2.             = Dec2Bin(15) + '1' + '0'                            # Dec2Bin(15) + '1' = Dec2Bin(31)
  3.             = Dec2Bin( 7) + '1' + '1' + '0'                      # Dec2Bin( 7) + '1' = Dec2Bin(15)
  4.             = Dec2Bin( 3) + '1' + '1' + '1' + '0'                # Dec2Bin( 3) + '1' = Dec2Bin( 7)
  5.             = Dec2Bin( 1) + '1' + '1' + '1' + '1' + '0'          # Dec2Bin( 1) + '1' = Dec2Bin( 3)
  6.             = Dec2Bin( 0) + '1' + '1' + '1' + '1' + '1' + '0'    # Dec2Bin( 0) + '1' = Dec2Bin( 1)
  7.             =      ''     + '1' + '1' + '1' + '1' + '1' + '0'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2020-2-28 23:12:05 | 显示全部楼层    本楼为最佳答案   
  1. def Dec2Bin(dec):
  2.     result = ''
  3.    
  4.     if dec: #如果dec不等于0
  5.         result = Dec2Bin(dec//2)
  6.         return result + str(dec%2)
  7. '''
  8.                result + str(dec%2)=Dec2Bin(62//2) + str(62%2)  
  9.                                   =Dec2Bin(31) + str(62%2)
  10.                                   =Dec2Bin(31//2) +str(31%2) + str(62%2)
  11.                                   =Dec2Bin(15) +str(31%2) + str(62%2)
  12.                                   =Dec2Bin(15//2) + str(15%2) +str(31%2) + str(62%2)
  13.                                   =Dec2Bin(7) + str(15%2) +str(31%2) + str(62%2)
  14.                                   =Dec2Bin(7//2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  15.                                   =Dec2Bin(3) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  16.                                   =Dec2Bin(3//2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  17.                                   =Dec2Bin(1) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  18.                                   =Dec2Bin(1//2)+ str(1%2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  19.                                   =Dec2Bin(0)+ str(1%2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
  20.                                   ='' +'1'+'1'+'1'+'1'+'1'+'0'
  21.                                   ='111110'
  22. '''
  23.     else:
  24.         return result

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

使用道具 举报

 楼主| 发表于 2020-2-29 12:00:32 | 显示全部楼层
jackz007 发表于 2020-2-28 22:50
为了讨论方便,先简化程序代码

      递归就是函数自己调用自己的行为,递归函数一般都有两个分支, ...

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 05:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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