题解 | 二进制数1
二进制数1
https://www.nowcoder.com/practice/bc4c7936f5ed42cbb9131b6f39aa272b
using System;
class Program
{
static void Main()
{
// 读取输入数字
ulong x = ulong.Parse(Console.ReadLine());
int count = 0;
// Brian Kernighan 算法:每次清除最右边的1
while (x != 0)
{
x &= x - 1;
count++;
}
// 输出结果
Console.WriteLine(count);
}
}
查看12道真题和解析
