题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String a;
StringBuffer bu = new StringBuffer();
int n, i, j, sum, max;
int[] biny;
try {
while ((a = in.readLine()) != null && !a.isEmpty()) {
n = parse1(a);
i = 1;
biny = new int[32];
biny[0] = n & 1;
while (i < 32) {
n = n >> 1;
biny[i] = n & 1;
i++;
}
i = 0;
max = 0;
while (i < 32) {
if (biny[i] == 1) {
j = i;
sum = 0;
while (j < 32) {
if (biny[j] == 1) sum++;
else break;
j++;
}
max = max < sum ? sum : max;
}
i++;
}
bu.append(max).append("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.print(bu);
}
public static int parse1(String a) {
char[] charAy = a.toCharArray();
int i = 0, n = 0, l = charAy.length;
while (i < l) {
n *= 10;
n += charAy[i] - '0';
i++;
}
return n;
}
}