801. 二进制中1的个数
摘要
Title: 801. 二进制中1的个数
Tag: 位运算
Memory Limit: 64 MB
Time Limit: 1000 ms
Powered by:NEFU AB-IN
801. 二进制中1的个数
-
题意
给定一个长度为 n 的数列,请你求出数列中每个数的二进制表示中 1 的个数。
-
思路
运用
lowbit
操作,每次返回自己最后一位1,然后把它减掉,能减多少次,就是答案 -
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18'''
Author: NEFU AB-IN
Date: 2022-02-23 16:02:45
FilePath: \ACM\Acwing\801.py
LastEditTime: 2022-02-23 16:02:46
'''
lowbit = lambda x: x & -x
n = int(input())
a = list(map(int, input().split()))
for i in a:
cnt = 0
while i:
i -= lowbit(i)
cnt += 1
print(cnt, end=" ")