64,654
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include<iostream>
using namespace std;
int solution(int n);
int main(int argc, char* argv[])
{
cout<<solution(10)<<endl;
return 0;
}
int solution(int n)
{
int count=0;
int flag=1;
while (flag)
{
if((n&flag) ==1)//为嘛改写这样就可以呢:if(n&flag)
count++;
flag=flag<<1;
}
return count;
}
#include "stdafx.h"
#include<iostream>
using namespace std;
int solution(int n);
int main(int argc, char* argv[])
{
cout<<solution(10)<<endl;
return 0;
}
int solution(int n)
{
int count=0;
int flag=1;
while (flag)
{
if((n&flag) ==1)//flag的初始是0x0001,从最右边的一位开始判断是否为1
count++;
flag=flag<<1;//从右到左,依次判断n的某一位是否为1
}
return count;
}