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

来源:互联网 发布:android 珍藏源码 编辑:程序博客网 时间:2024/05/16 11:26

描述
功能: 求一个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;}
0 0
原创粉丝点击