华为OJ——求最大连续bit数

    xiaoxiao2025-04-27  12

    求最大连续bit

    题目描述

    功能求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续21      输入一个byte型的数字      输出       返回对应的二进制数字中1的最大连续数

    输入描述:

    输入一个byte数字

    输出描述:

    输出转成二进制之后连续1的个数

    输入例子:

    3

    输出例子:

    2

    解答代码:

    #include<iostream> #include<fstream> #include<string> #include<cstring> #include<algorithm> #include<sstream> using namespace std; int main() { int n; while(cin>>n) { int i,count=0; int flag=1; int max=0; do { if(n%2==1) { count++; } else { count=0; } n/=2; if(count>max) max=count; } while(n!=0); cout<<max<<endl; } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1298514.html
    最新回复(0)