输入一个十进制整数
。
输出一个整数,表示
的二进制表示中,最长连续
段的长度。
200
2
在这个样例中,十进制
等于二进制
,其中最长连续
段的长度为
。
1023
10
在这个样例中,十进制
等于二进制
。
本题数据已规范为单组询问(2025/01/15)。
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); } } }
import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { int n = Integer.parseInt(str); //临时记录连续1的个数 int temp = 0; // 记录最长连续1的个数 int maxOne = 0; //记录循环次数 int i = 0; //int类型共32位,循环32次 while (i <= 31) { // 肉眼可见的十进制的n在机器里是二进制32位0和1,n & 1可以得到得出二进制的 // 末位为1或者0,如果为0则将临时记录连续1的个数置为0 if ((n & 1) == 0) temp = 0; // 如果为1则将临时记录连续1的个数+1 else temp++; if (maxOne < temp) maxOne = temp; // 每次循环都将数据n二进制末位舍弃,即右移1位 n = n >> 1; i++; } System.out.println(maxOne); } } }
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 s = Integer.toBinaryString(a); int count = 0; int max = 0; for (int i = 0; i < s.length(); i++) { String str = String.valueOf(s.charAt(i)); if (str.equals("1")) { max ++; count = Math.max(count, max); } else { max = 0; } } System.out.println(count); } }
public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine()); int n = 0; int i = 0; while(num > 0) { i = 0; while((num & 1) == 1) { //末位为1时,连续右位移 i ++; num = num >> 1; } n = Math.max(n, i); num = num >> 1; } System.out.println(n); }