题解 | #求int型正整数在内存中存储时1的个数#
求int型正整数在内存中存储时1的个数
https://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int a, b=32,c=0;
cin >> a;
while(a > 0){
//从最高位开始找1
if ((a - pow(2,b)) >= 0)
{
a -= pow(2,b);
c++;
}
b--;
}
cout << c << endl;
return 0;
}

查看14道真题和解析