题解 | #求最大连续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) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while (null != (s = br.readLine())) {
int x = Integer.valueOf(s);
int res = 0;
int count = 0;
while (x != 0) {
if ((x & 1) == 1) {
count++;
res = count > res? count : res;
} else {
count = 0;
}
x = x >> 1;
}
System.out.println(res);
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while (null != (s = br.readLine())) {
int x = Integer.valueOf(s);
int res = 0;
int count = 0;
while (x != 0) {
if ((x & 1) == 1) {
count++;
res = count > res? count : res;
} else {
count = 0;
}
x = x >> 1;
}
System.out.println(res);
}
}
}