|  | 
 
 发表于 2020-2-28 23:12:05
|
显示全部楼层
   本楼为最佳答案 
| 复制代码def Dec2Bin(dec):
    result = ''
   
    if dec: #如果dec不等于0
        result = Dec2Bin(dec//2) 
        return result + str(dec%2)
'''
               result + str(dec%2)=Dec2Bin(62//2) + str(62%2)  
                                  =Dec2Bin(31) + str(62%2)
                                  =Dec2Bin(31//2) +str(31%2) + str(62%2)
                                  =Dec2Bin(15) +str(31%2) + str(62%2)
                                  =Dec2Bin(15//2) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(7) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(7//2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(3) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(3//2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(1) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(1//2)+ str(1%2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
                                  =Dec2Bin(0)+ str(1%2) + str(3%2) + str(7%2) + str(15%2) +str(31%2) + str(62%2)
                                  ='' +'1'+'1'+'1'+'1'+'1'+'0'
                                  ='111110'
'''
    else:
        return result
print(Dec2Bin(62))
        
 | 
 |