题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
'''十进制转二进制: 5=====101 1*2^0 + 0*2^1 + 1*2^2 = 1+0+4=5 5/2=2---1 2/2=1---0 1/2=0---1 7=====111 1*2^0 + 1*2^1 + 1*2^2 + 1+2+4=7 7/2=3---1 3/2=1---1 1/2=0---1 2/2=1---0 10 1/2=0---1 4/2=2---0 100 2/2=1---0 1/2=0---1 print('10101'.count('1')) print([1,2,3].count(1)) ''' while True: try: n=int(input()) binary='' while n//2>=1: binary = binary+str(n%2) n = n//2 binary = binary+str(n) binary = binary[::-1] print(binary.count('1')) except: break