华为oj初级 求最大连续bit数

    xiaoxiao2021-03-25  82

    描述 功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1

    输入: 一个byte型的数字

    输出: 无

    返回: 对应的二进制数字中1的最大连续数 知识点 位运算 运行时间限制 10M 内存限制 128 输入 输入一个byte数字 输出 输出转成二进制之后连续1的个数 样例输入 3 样例输出 2

    #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int main(){ int n; cin >> n; int max = 0,thisMax = 0; while (n){ if(n & 0x1u) thisMax++; else{ if (thisMax > max){ max = thisMax; thisMax = 0; } } n >>= 1; } cout <<thisMax; return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-34056.html

    最新回复(0)