输入一个十进制整数
。
输出一个整数,表示
的二进制表示中,最长连续
段的长度。
200
2
在这个样例中,十进制
等于二进制
,其中最长连续
段的长度为
。
1023
10
在这个样例中,十进制
等于二进制
。
本题数据已规范为单组询问(2025/01/15)。
用了十进制转二进制函数Integer.toBinaryString(Integer.valueOf(str)),随后从前往后遍历二进制数,用count自增连续的1,同时用max记录最大值,当遍历到零,count重新从0开始计数,7.18
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.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
String biaryString = Integer.toBinaryString(Integer.valueOf(str));
int count = 0,max = 0;
for (char c : biaryString.toCharArray()){
if (c == '1'){
count++;
max = Math.max(max, count);
}else{
count = 0;
}
}
System.out.println(max);
}
}
} 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 a = in.nextInt();
System.out.println(get(a));
}
}
public static int get(int a){
int count =0;
int maxLength = 0;
while(a!=0){
int temp=a&1;
if(temp ==1){
maxLength = Math.max(maxLength,++count);
}else{
count =0;
}
a=a>>1;
}
return maxLength;
}
} import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); //转化为二进制字符串 String s=Integer.toBinaryString(n); //lenMax:一个存储只含1的字符串 String lenMax=""; //逐步增加一个只含1的字符串的长度,然后去看目标字符串中有没有,没有就输出lenMax的长度,跳出来 for(int i=0;i<32;i++){ lenMax=lenMax+1; if(!s.contains(lenMax)){ System.out.print(i); break; } } } }
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 a = in.nextInt();
System.out.println(longestBits(a));
}
}
private static int longestBits(int a) {
String bin = Integer.toBinaryString(a);
int longest = 0;
int len = 0;
for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == '0') {
len = 0;
continue;
}
len++;
longest = Math.max(longest, len);
}
return longest;
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Integer num = in.nextInt();
String binStr = Integer.toBinaryString(num);
int res = 0;
int count = 0;
for (int i = 0; i < binStr.length(); i++) {
if (binStr.charAt(i) == '0') {
count = 0;
continue;
}
count ++;
res = Math.max(res, count);
}
System.out.print(res);
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int input = in.nextInt();
String bin = Integer.toBinaryString(input);
int count = 0;
int res = 0;
for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == '1') {
count++;
}else{
count = 0;
}
res = Math.max(res, count);
}
System.out.println(res);
}
}
} public static void main(String[] args){ Scanner sc = new Scanner(System.in); int m = sc.nextInt(); String s = Integer.toBinaryString(m); int left = 0; int right = left + 1; int len = 0; while (right <= s.length()) { String str = s.substring(left, right); if (str.replace("1", "").length() == 0) { len = Math.max(len, str.length()); right++; } else { left++; right = left + 1; } } System.out.println(len); }
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
String string = Integer.toBinaryString(num);
String[] s = string.split("0");
int max=0;
for (int i = 0; i < s.length; i++) {
s[i]=s[i].replace(" ","");
max=Math.max(s[i].length(),max);
}
System.out.println(max);
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=sc.nextInt();
StringBuilder sb=new StringBuilder("");
for(int i=31;i>=0;i--){
char c=(num&(1<<i))==0?'0':'1';
sb.append(c);
}
String []s=sb.toString().split("0");
int max=0;
for(String str:s){
if(str.length()>max){
max=str.length();
}
}
System.out.print(max);
}
} import java.util.Scanner;
import java.math.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = Integer.toBinaryString(in.nextInt());
int []dp = new int[s.length() + 1];
int max = 0;
for (int i = 1; i <= s.length(); i++) {
if (s.charAt(i - 1) == '1') {
dp[i] = dp[i-1] + 1;
max = Math.max(dp[i], max);
}
}
System.out.println(max);
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int a = in.nextInt();
String b = Integer.toBinaryString(a);
int max=0;
int thisMax=0;
for(int i=0;i<b.length();i++){
char c = b.charAt(i);
if(c == '1'){
thisMax++;
}else{
thisMax=0;
}
max = Math.max(max,thisMax);
}
System.out.print(max);
}
}
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
int number = Integer.parseInt(line);
String binary = Integer.toBinaryString(number);
int maxLength = 0;
for (int i = 0; i < binary.length(); i++) {
for (int j = i + 1; j <= binary.length(); j++) {
String subStr = binary.substring(i, j);
if (!subStr.contains("0") && subStr.length() > maxLength) {
maxLength = subStr.length();
}
}
}
System.out.println(maxLength);
}
}
}