目录
一、题目
二、代码
一、题目
求最大连续bit数_牛客题霸_牛客网
二、代码
#include <iostream>
#include<stack>
#include<vector>
using namespace std;
void TEN_to_TWO(int x, vector<int>& data) { //10进制转换成二进制
stack<int> s;
while (x != 0) {
int remainder = x % 2;//余数
s.push(remainder);
x /= 2;
}
while (!s.empty()) {
data.push_back(s.top());
s.pop();
}
}
int main() {
int x;
cin >> x;
vector<int>data;
TEN_to_TWO(x, data);
int left = 0, right = 0;
bool mask = false;
int _MAX = 0;
while (right < data.size()) {
while (right < data.size() && data[right] == 1) {
if ( mask == false) {
left = right;
mask = true;
}
++right;
}
if (data[right - 1] == 1)
{
_MAX = max(_MAX, right - left);
mask = false;
}
++right;
}
cout << _MAX;
return 0;
}