题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
#include <bits/stdc++.h>
using namespace std;
int main() {
int a;
while (cin >> a) { // 注意 while 处理多个 case
bitset<32> bits(a);
auto s = bits.to_string();
int count = 0;
for (int i = 0;i < s.size();i++) {
if (s.at(i) == '0') {
// count = 0;
} else {
int t = 0;
int j = i;
while (j < s.size() && s.at(j) == '1') {
t++;
j++;
}
count = max(count, t);
}
}
cout << count << endl;
}
}

查看8道真题和解析