题解 | #求最大连续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;
    }
}

全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务