问题1:把十进制数转换成二进制数
思路:先找到v=2^k使其最近接n,再从n中减去v并不断将v减半,n<v时记1,n>1时记0
n = 13 b = '' v = 1 while v <= n//2: v *= 2 while v > 0: if n < v: b += '0' else: b += '1' n -= v v //= 2 print(b)
问题1:把十进制数转换成二进制数
思路:先找到v=2^k使其最近接n,再从n中减去v并不断将v减半,n<v时记1,n>1时记0
n = 13 b = '' v = 1 while v <= n//2: v *= 2 while v > 0: if n < v: b += '0' else: b += '1' n -= v v //= 2 print(b)