题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
while True: try: n = int(input()) bit_num = bin(n).lstrip('0b') num_length = len(bit_num) dp = [0 for i in range(num_length)] if bit_num[0] == '1': dp[0] = 1 for i in range(1, num_length): if bit_num[i] == '1' and bit_num[i-1] == '1': dp[i] = dp[i-1] + 1 elif bit_num[i] == '1' and bit_num[i - 1] == '0': dp[i] = 1 else: dp[i] = dp[i-1] print(max(dp)) except: break
伪动态分配,多加了一个条件,遇到01就设状态空间值为1,然后求所有状态空间的最大值,即为所需结果。