方法1:python

while True:
    try:
        a = bin(int(input()))
        maxnumstr = a.count('1')
        maxnum = int(maxnumstr)
        num = 0
        for i in range(maxnum):
            if '1' * (i + 1) in a:
                num = i + 1
            else:
                break
        print(num)
    except:
        break

方法2:python

while True:
    try:
        n = int(input())
        c = 0
        m = 0
        while n:
            if (n % 2):
                c += 1
                if c > m:
                    m = c
            else:
                c = 0
            n = int(n/2)
        print(m)
    except:
        break

方法3: 最优解法
python

while True:
    try:
        number = int(input())
        str_num = bin(number)
        print(len((max(str(str_num).replace("0b","").split("0")))))
    except:
            break