题解 | #求最大连续bit数#
求最大连续bit数
http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int i = in.nextInt();
String binaryString = Integer.toBinaryString(i);
char[] chars = binaryString.toCharArray();
int max = 0;
int count = 0;
for (int j = 0; j < chars.length; j++) {
if (chars[j] == '1') {
count++;
if (j == chars.length - 1)
max = Math.max(max, count);
continue;
}
max = Math.max(max, count);
count = 0;
}
System.out.println(max);
}
}
}