首页 > 试题广场 >

求最大连续bit数

[编程题]求最大连续bit数
  • 热度指数:148280 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的十进制整数 n,求解其二进制表示中,最长连续 1 段的长度。

输入描述:
\hspace{15pt}输入一个十进制整数 n \left(1 \leqq n \leqq 5 \times 10^5\right)


输出描述:
\hspace{15pt}输出一个整数,表示 n 的二进制表示中,最长连续 1 段的长度。
示例1

输入

200

输出

2

说明

\hspace{15pt}在这个样例中,十进制 (200)_{10} 等于二进制 (11\,001\,000)_{2},其中最长连续 1 段的长度为 2
示例2

输入

1023

输出

10

说明

\hspace{15pt}在这个样例中,十进制 (1023)_{10} 等于二进制 (1\,111\,111\,111)_{2}

备注:
\hspace{15pt}本题数据已规范为单组询问(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;
    }
}

发表于 2025-02-06 13:18:47 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        String str = Integer.toBinaryString(a);
        char[] strChar = str.toCharArray();
        int n = 0;
        int max =0;
        for (int i = 0; i < strChar.length; i++) {
            if (strChar[i] == '1' ) {
                n++;
                max = Math.max(n,max);
            }else{
                n =0;
            }
        }
        System.out.print(max);
    }
发表于 2024-12-12 19:24:24 回复(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();
            int maxCount = 0;
            int currentCount = 0;
            while (a > 0) {
                if (a % 2 == 0) {
                    maxCount = Math.max(maxCount, currentCount);
                    currentCount = 0;
                } else {
                    currentCount ++;
                }
                a = a>>1;
            }
            System.out.println(Math.max(maxCount, currentCount));
        }
    }
}
发表于 2024-11-24 22:55:42 回复(0)
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);
    }
}

发表于 2024-10-06 23:22:56 回复(0)
转成2进制之间按0分割,再找出长度最大的部分
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int str=in.nextInt();
        String str1=Integer.toBinaryString(str);
        String[] str2=str1.split("0");
        int length=str2.length;
        int n=0;
        for(int i=0;i<length;i++){
            if(str2[i].length()>=n){
                n=str2[i].length();
            }
        }
        System.out.println(n);
    }
}

发表于 2024-08-27 16:42:20 回复(0)
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);
        }
    }
}

发表于 2024-08-25 19:45:32 回复(0)

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); }
发表于 2024-08-19 23:40:37 回复(0)
思路:直接int十进制转二进制,然后根据0分隔字符得到字符串数组,然后遍历数组求得最大字符串长度
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);
    }
}


发表于 2024-06-25 21:38:45 回复(0)
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)