题解 | #HJ86 求最大连续bit数#
求最大连续bit数
http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
C语言版本
#include <stdio.h>
int main() {
int n, rangeMax, rangeCur;
while(scanf("%d", &n) != EOF) {
rangeMax = 0, rangeCur = 0;
while (n > 0) {
if ((n & 0x1) == 0x1) {
rangeCur++;
if (rangeCur > rangeMax) {
rangeMax = rangeCur;
}
} else {
rangeCur = 0;
}
n >>= 1;
}
printf("%d\n", rangeMax);
}
return 0;
}
Python版本
while True:
try:
n = int(input())
binStr = bin(n)[2:]
temp = binStr.split('0')
print(max(map(len, temp)))
except:
break