题解 | #求最大连续bit数#明明可以更简单
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
#include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n) { int count = 0;//记录每次统计的连续1的个数 int max_count = 1;//记录最大连续1的个数 while (n) { if (n % 2 == 1){//最后一位为1 count++; max_count = max(max_count, count);//更新最大值 } else //遇到不为1 count = 0;//从0开始 n /= 2; //去掉最后一位 } cout << max_count << endl; } return 0; }