首页 > 试题广场 >

求最大连续bit数

[编程题]求最大连续bit数
  • 热度指数:137524 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1

数据范围:数据组数:
进阶:时间复杂度:,空间复杂度:

输入描述:

输入一个int类型数字



输出描述:

输出转成二进制之后连续1的个数

示例1

输入

200

输出

2

说明

200的二进制表示是11001000,最多有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);
    }
}


发表于 2024-04-11 08:48:37 回复(0)
简单易懂
Scanner in = new Scanner(System.in);
        int i = in.nextInt();
        String s = Integer.toBinaryString(i);
        String[] split = s.replaceAll("0", " ").split(" ");
        Arrays.sort(split);
        System.out.println(split[split.length - 1].length());
编辑于 2024-03-11 16:34:27 回复(0)
这题用动态规划不是可以写吗



编辑于 2024-02-13 14:35:04 回复(0)
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);
    }
}

发表于 2023-11-28 12:58:11 回复(0)
import java.util.*;

// 注意类名必须为 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();
            String s = Integer.toString(a, 2);
            String [] arr = s.split("0");
            int max = 0;
            for (String t : arr) {
                max = Math.max(max, t.length());
            }
            System.out.println(max );
        }
    }
}
发表于 2023-09-06 14:36:11 回复(0)
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);
    }
}
发表于 2023-09-04 18:53:07 回复(0)
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);
        }
    }
}

发表于 2023-08-13 16:25:04 回复(0)
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();
       int n=0;
       String b=Integer.toString(a,2);
       String[] c=b.split("0");
       for(int i=0;i<c.length;i++)
       {int m=c[i].length();
        if(n<m)
        {n=m;}
       }
       System.out.print(n);
    }
}
发表于 2023-07-13 18:04:24 回复(1)
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);
        }
    }
}

发表于 2023-07-10 13:08:31 回复(0)
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);
    }
}

发表于 2023-06-08 10:23:17 回复(0)
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);
    }

发表于 2023-05-20 20:28:17 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String s = in.nextLine();
        s = Integer.toBinaryString(Integer.parseInt(s));
        int num = getLongest(s, 0, 1, 1);
        System.out.println(num);
    }

    public static int getLongest(String str, int index, int curMaxLen, int maxLen) {
        //先考虑递归退出条件,index=str.length()-1;
        if("0".equals(str)) return 0;
        if (index == str.length() - 1) {
            return Math.max(curMaxLen, maxLen);
        }
        //两个连续字符相同且都是1的话
        if (str.charAt(index) == str.charAt(index + 1) && str.charAt(index) == '1') {
            //curMaxLen + 1:当前字符串中最长连续1加1
            return getLongest(str, index + 1, curMaxLen + 1, maxLen);
        } else {
            //注意maxlen只有在字符不一致的时候会去取大
            //字符不同,将当前连续最大值和总体最大值作比较
            return getLongest(str, index + 1, 1, Math.max(maxLen, curMaxLen));
        }
    }

}
发表于 2023-04-04 16:39:18 回复(0)
右移
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;
    }
}


发表于 2023-03-16 14:19:27 回复(0)
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);
        }
    }

}

发表于 2023-03-04 17:14:33 回复(0)
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;
    }
}

发表于 2023-02-09 15:21:02 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        if (in.hasNextLine()) {
            String[] split = Integer.toBinaryString(in.nextInt()).split("[0]+");
            int max=0;
            for (String s1 : split) {
                if (s1.length()>max){
                    max=s1.length();
                }
            }
            System.out.println(max);
        }
    }
}
发表于 2023-01-13 21:19:15 回复(0)
遍历
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;
    }
}

发表于 2023-01-08 19:37:29 回复(0)
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]);
        }
    }
}

发表于 2022-10-29 03:06:37 回复(0)

问题信息

难度:
71条回答 28231浏览

热门推荐

通过挑战的用户

查看代码