题解 | #求最大连续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 in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int count=0;
            //把a转成二进制串
            String binary=Integer.toBinaryString(a);
            //遍历处理
            int max=0;
            for(int i=0;i<binary.length();i++){
                char ch=binary.charAt(i);
                if(ch=='1'){
                    count++;
                    if(count>max){
                        max=count;
                    }
                }
                if(i<binary.length()-1){
                    if(binary.charAt(i+1)!='1'){
                        i++;
                        count=0;
                    }   
                }

            }
            System.out.println(max);
        }
    }
}

java做进制转换简直不要太爽

全部评论

相关推荐

代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
06-13 17:00
武汉大学 Java
6月了还有点击就送的offer吗😭,投麻了😢
叫我阿东就行:这个bg,也还没找到理想的工作吗?好难,好焦虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务