题解 | #求int型正整数在内存中存储时1的个数#
求int型正整数在内存中存储时1的个数
http://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9
#include <iostream>
#include <string>
#include <algorithm>
//#include <math.h>
using namespace std;
void process(int num, int& res){
while(num != 0){
res++;
num &= (num - 1);
}
}
int main(){
int num = 0;
cin>>num;
int res = 0;
process(num, res);
cout<<res<<endl;
return 0;
}
华为题库题解 文章被收录于专栏
牛客华为题库的题解