求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
数据范围:数据组数:,
进阶:时间复杂度:,空间复杂度:
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); }
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(countOne(a)); } } private static int countOne(int num) { // 定义一个maxCount, 存放最大连续长度 int maxCount = 0; for (int i = 0; i < 32;) { int count = 0; int j = 0; while ((num & 1) == 1) { count ++; num = num >>> 1; j ++; } i = i + j + 1; num = num >>> 1; maxCount = Math.max(maxCount, count); } return maxCount; } }
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int count = 0; int max = 0; while (n != 0) { if ((n & 1) == 1) count++; else count = 0; if (count > max) max = count; n >>= 1; } System.out.println(max); } } }
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(getOne(a)); } } public static int getOne(int a) { String s = Integer.toBinaryString(a); int len = s.length(); int num = 0; int temp = 0; for (int i = 0; i < len; i++) { if (s.charAt(i) == '1') { temp++; } else { temp = 0; } num = Math.max(temp, num); } return num; } }
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 n = in.nextInt(); String s = Integer.toBinaryString(n); int max = 0; for(int i = 0;i<s.length();i++){ for(int j = s.length();j>i;j--){ String sub = s.substring(i,j); if(allIsOne(sub)){ max = Math.max(max,j-i); } } } System.out.print(max); } public static boolean allIsOne(String s ){ char[] array = s.toCharArray(); for(int i = 0;i<array.length;i++){ if(array[i]!='1'){ return false; } } return true; } }
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); String str = Integer.toBinaryString(n); String[] a = str.split("0"); int l = a.length; int[] arr = new int[l]; for (int i = 0; i < l; i++){ arr[i] = a[i].length(); } Arrays.sort(arr); System.out.print(arr[l-1]); } } }