题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
#include <iostream> using namespace std; int main() { int num; cin >> num; int cnt = 0; int maxC = 0; while (num != 0) { if (num % 2 == 1) { cnt++; } else { cnt = 0; } num = num / 2; maxC = max(maxC, cnt); } cout << maxC << endl; }
转化为2进制时顺便计数