zoukankan      html  css  js  c++  java
  • 求最大连续bit数

    描述

    功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
        
    输入: 一个byte型的数字
        
    输出: 无
         
    返回: 对应的二进制数字中1的最大连续数

    知识点 位运算
    运行时间限制 10M
    内存限制 128
    输入

    输入一个byte数字

    输出

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

    样例输入 3
    样例输出 2
    package com.oj;
    
    import java.util.HashMap;
    import java.util.Scanner;
    
    public class Test2 {
    	
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		int num = in.nextInt();
    		String binary = Integer.toBinaryString(num);
    		//System.out.println(binary);
    		int max = 0;
    		int count = 0;
    		for(int i = 0;i < binary.length(); i++)
    			if(binary.charAt(i)=='0'){
    				if(count>max){
    					max = count;
    					count = 0;
    				}
    			}else if(binary.charAt(i)=='1'&&i!=binary.length()-1){
    				count++;
    			}else if(binary.charAt(i)=='1'&&i==binary.length()-1){
    				count++;
    				if(count>max)
    					max = count;
    			}
    		System.out.println(max);
    	}
    }
    

      

  • 相关阅读:
    文章索引
    Rancher pipeline 实现简单跟踪
    rancher 应用商店
    nginx ingress 在aks 上安装
    go countdown
    go channel pipeline 套路
    gorm使用
    华为云cce pvc 指定云硬盘云存储
    influxdb 基本概念
    Python3处理xlsx去掉含有特定字符的行
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/5381367.html
Copyright © 2011-2022 走看看