题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
#include <climits> #include <iostream> #include <vector> using namespace std; vector<int> TenToTwo(int num) { vector<int> vec; int tmp; while (num != 0){ if (num % 2 != 0){ tmp = 1; } else{ tmp = 0; } vec.push_back(tmp); num = num / 2; } vector<int> vv; for (int i = vec.size(); i >= 0; i--){ vv.push_back(vec[i]); // cout << vec[i]; } return vv; } int main() { int input; cin >> input; vector<int> VV = TenToTwo(input); int max = INT_MIN; int cnt = 1; for (int i = 0; i < VV.size(); i++){ if (VV[i] == 1 && VV[i+1] == 1){ cnt++; } else{ cnt = 1; } if (max < cnt) max = cnt; } cout << max <<endl; return 0; } // 64 位输出请用 printf("%lld")