题解 | #求最大连续bit数#

求最大连续bit数

http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2

#include <iostream>
#include <regex>
#include <algorithm>
using namespace std;
string Dec2bBin(int n)
{
    string s = "";
    while(n!=0)
    {
        s += to_string(n%2);
        n/=2;
    }
    reverse(s.begin(),s.end());
    return s;
}

int main()
{
    int a; cin >> a;
    string str = Dec2bBin(a);
    regex reg("1+"); 
    size_t iMaxLen = 0; 
    for(sregex_iterator itr(str.begin(),str.end(),reg),end_itr; itr!=end_itr; ++itr)
    {
        string st = itr->str();
        iMaxLen = max (iMaxLen, st.length());
    }
    cout << iMaxLen;
    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务