HJ86 求最大连续bit数

知识点位运算

描述

对于给定的十进制整数 n,求解其二进制表示中,最长连续 1 段的长度。

输入描述:

输入一个十进制整数 n(1≦n≦5×10(5次幂))。

输出描述:

输出一个整数,表示 n 的二进制表示中,最长连续 1 段的长度。

示例1

输入:

复制代码
200

输出:

复制代码
2

说明:

复制代码
在这个样例中,十进制 (200)10 等于二进制 (11 001 000)2​,其中最长连续 1 段的长度为 2。

示例2

输入:

复制代码
1023

输出:

复制代码
10

说明:

复制代码
在这个样例中,十进制 (1023)10 等于二进制 (1 111 111 111)2​。

备注:

复制代码
本题数据已规范为单组询问(2025/01/15)。

cc方法00:

/hj086_bits_max_00_vio_00.cc

cpp 复制代码
// 暴力枚举
#include <iostream>
#include <vector>

using namespace std;

int GetMaxBit(int & n)
{
    int cntMax = 0;
    int count = 0;

    while (n) {
        if (n % 2) {
            count++;
            cntMax = max(cntMax, count);
        } else {
            count = 0;
        }
        n = n / 2;
    }

    return cntMax;
}

int main()
{
    int n = 0;
    while (cin >> n) {
        cout << GetMaxBit(n) << endl;
    }

    return 0;
}

cc方法01:

/hj086_bits_max_00_vio_01.cc

cpp 复制代码
// 暴力枚举
#include <iostream>
#include <vector>

using namespace std;

int GetMaxBit(int & n)
{
    int count = 0;
    for (; n != 0; count++) {
        // 只有当 `i` 位和 `i+1` 位同时为1时,结果位才为1
        // - 任何孤立的1(右侧是0)会被消除
        // 连续1序列的尾部每次右移异或后会被截断一个,同时count++,全部截掉后连续1的个数就是count计数
        n &= n << 1;
    }

    return count;
}

int main()
{
    int n = 0;
    while (cin >> n) {
        cout << GetMaxBit(n) << endl;
    }

    return 0;
}

cc方法02:

/hj086_bits_max_00_vio_02.cc

cpp 复制代码
// 暴力枚举
#include <iostream>
#include <vector>

using namespace std;

int GetMaxBit(int & n)
{
    int count = 0;
    int cntMax = 0;
    for (; n != 0; ) {
        if (n & 1) {
            count++;
            cntMax = max(cntMax, count);
        } else {
            count = 0;
        }
        n = n >> 1;
    }

    return cntMax;
}

int main()
{
    int n = 0;
    while (cin >> n) {
        cout << GetMaxBit(n) << endl;
    }

    return 0;
}

cc方法03:

/hj086_bits_max_01_stl_00.cc

cpp 复制代码
// stl
#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>

using namespace std;

int parse(string s)
{
    vector<int> vec;
    char ch = '1';
    int count = 0;
    for (auto it = s.begin(); it != s.end(); ++it) {
        if (*it == ch) {
            count++;
            if (it == s.end() - 1) {
                vec.push_back(count);
            }
        } else {
            if (count > 0) {
                vec.push_back(count);
            }
            count = 0;
        }
    }
    sort(vec.begin(), vec.end());

    return vec.back();
}

int GetMaxBit(const int & n)
{
    bitset<32> bs(n);
    string s = bs.to_string('0', '1');
    return parse(s);
}

int main()
{
    int n = 0;
    while (cin >> n) {
        cout << GetMaxBit(n) << endl;
    }

    return 0;
}
相关推荐
不做无法实现的梦~2 分钟前
MAVLink 协议教程
linux·stm32·嵌入式硬件·算法
.千余26 分钟前
【C++】C++类与对象3:const成员函数与取地址运算符重载,权限管理的艺术
开发语言·c++
墨白曦煜28 分钟前
算法实战笔记:剥开回溯算法的外衣——从通用模板到高阶去重(八)
笔记·算法
z2005093040 分钟前
今日算法(回溯子集)(模版题)
数据结构·算法·leetcode
吴佳浩43 分钟前
Vibe Coding 时代,研发经理为何越来越值钱?
算法·架构
IronMurphy1 小时前
【算法五十四】72. 编辑距离
算法
QiLinkOS1 小时前
【用呼吸重构创造价值关系——QiLink生态】
c语言·数据结构·c++·人工智能·单片机·嵌入式硬件·算法
朔北之忘 Clancy1 小时前
2026 年 3 月青少年软编等考 C 语言二级真题解析
c语言·开发语言·c++·学习·青少年编程·题解·考级
妄想出头的工业炼药师1 小时前
暗光长走廊特殊场景视觉解决方案
算法·开源
weixin_468466851 小时前
图像处理特征提取新手实战指南
图像处理·人工智能·算法·ai·机器视觉·特征提取