题解 | #求最大连续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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务