题解 | #求最大连续bit数#

求最大连续bit数

https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        if(num == 0){
            System.out.println(0);
        }
        else {
            StringBuilder strB = new StringBuilder("");
            while (num > 0){
                strB.append(num%2);
                num /= 2;
            }
            int max = 0;
            int count = 0;
            for (int i = 0; i < strB.length(); i++) {
                if (strB.charAt(i) == '1'){
                    count++;
                }
                else {
                    count = 0;
                }
                max = Math.max(count,max);
            }
            System.out.println(max);
        }
    }
}

全部评论

相关推荐

03-25 19:00
东北大学 Java
程序员牛肉:太好了,是聊天记录。不得不信了。 当个乐子看就好,不要散播焦虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务