#include <iostream>
using namespace std;

int main() {
    int n;cin>>n;
    int res = 0;
    while(n){
        if(n&1){
            int cnt = 1;
            while((n>>=1)&1&&n){
                cnt++;
            }
            res = max(res,cnt);
        }
        n>>=1;

    }
    cout<<res<<'\n';
    return 0;
}
// 64 位输出请用 printf("%lld")

使用位运算,让n左移,然后与1判断,输出最长的即可。

活动地址https://www.nowcoder.com/discuss/726480854079250432