思路:位运算;
代码如下
# include <stdio.h>
# include <iostream>
# include <algorithm>
using namespace std;
int lowbit(int x)
{
return x&(-x);
}
int main()
{
int t,a;
scanf("%d",&t);
while(t--)
{
scanf("%d",&a);
if(a==1||a==0)
{
printf("0\n");
continue ;
}
int d=0;
while(1)
{
int m=a&(a-1);
if(m==0)
{
break;
}
int y=lowbit(a);
a+=y;
d++;
}
printf("%d\n",d);
}
}
知识点:(1) a&(a-1)用来判断是否为该数是否为为2的倍数;
如果为零则是,否则不是;
(2)x&(-x)
截取该数最低位1所对应的值并把它转化为10进制位数;