描述 请实现如下接口 public static int findNumberOf1(intnum) { /* 请实现 */ return 0; }譬如:输入5 ,5的二进制为101,输出2
涉及知识点:
位运算
知识点 位运算 运行时间限制 10M 内存限制 128 输入 输入一个整数 输出 计算整数二进制中1的个数 样例输入 5 样例输出 2
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
int n;
cin >> n;
int cnt =
0;
while (n){
n = n&(n -
1);
cnt++;
}
cout << cnt << endl;
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-15671.html