def my_bin(num):
la = []
if num < 0:
return '-' + my_bin(abs(num))
while True:
num, remainder = divmod(num, 2)
la.append(str(remainder))
if num == 0:
return ''.join(la[::-1])
print my_bin(5)
print my_bin(0)
print my_bin(-5)
输出结果:
def my_bin(num):
la = []
if num < 0:
return '-' + my_bin(abs(num))
while True:
num, remainder = divmod(num, 2)
la.append(str(remainder))
if num == 0:
return ''.join(la[::-1])
print my_bin(5)
print my_bin(0)
print my_bin(-5)
输出结果: