ACcode:
/*
author:nttttt;
add oil!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<cmath>
#include<vector>
#include<functional>//使用 greater<int>();
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
int arry[N];
int pre[N];
int rank[N];
int va[N];
int lowbit(int x)
{
return x&(-x);
}
int judge(int x)
{
return x&(x-1);
}
int main()
{
//cout << lowbit(6) << endl;
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int res = 0;
while(judge(n))
{
n += lowbit(n);
res++;
}
printf("%d\n",res);
}
return 0;
}
官方题解中提到了 lowbit()函数 ,去简单了解一下
👆参考博客
再来可以用来判断x是否为2的幂数!
int judge(int x)
{
return x&(x-1);
}