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

求最大连续bit数

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while (null != (s = br.readLine())) {
            int x = Integer.valueOf(s);
            int res = 0;
            int count = 0;
            
            while (x != 0) {
                if ((x & 1) == 1) {
                    count++;
                    res = count > res? count : res;
                } else {
                    count = 0;
                }
                x = x >> 1;
            }
            System.out.println(res);
        }
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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