题解 | 求最大连续bit数
#include <bitset> #include <iostream> using namespace std; int main() { int n; cin >> n; bitset<32> bs(n); string n_str = bs.to_string(); // 双指针,贪心 int left = 0; int right = 0; int max_num1 = 0; for (int i = 0; i < n_str.size(); i ++) { if (n_str[i] == '0') { continue; } else { left = i; right = i; while (n_str[right] != '0' && right < n_str.size()) { right ++; } int len = right - left; if (len >= max_num1) { max_num1 = len; } i = right; } } cout << max_num1 << endl; } // 64 位输出请用 printf("%lld")