华为机试 HJ62题解 | #查找输入整数二进制中1的个数#
查找输入整数二进制中1的个数
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad
思路:直接使用bitset的count()方法,統計二進制中1的位數
#include <iostream>
#include <bitset>
using namespace std;
int main() {
int n;
int count = 0;
while (cin >> n) { // 注意 while 处理多个 case
bitset<32> b(n);
cout << b.count() << endl;
}
}
]
CVTE公司福利 732人发布
