1,终于不是数列啦,乖乖
2.这道题代码很简单,但是思路很有意思,举个栗子,3的二进制:0011,3-1的二进制:0010,3与3-1按位与:0010,递归直到0000,那么能与几次,这个数的二进制就有几个1。
class Solution
{
private static int NumberOf1(int n)
{
int count = 0;
while (n != 0)
{
count++;
n &= n - 1;
}
return count;
}
static void Main(string[] args)
{
Console.Write(NumberOf1(5));
}
}