求最大连续bit数
| 描述 | 
 功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个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);
}
}
求最大连续bit数的更多相关文章
- java笔试之求最大连续bit数
		
功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 无 返回: 对应的二进制数字中1 ...
 - 华为OJ平台——求最大连续bit数
		
题目描述: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 对应的二进制数字中1的最大连续数 思路: ...
 - Magical GCD UVA 1642 利用约数个数少来优化 给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量的值最大。输出这个最大值。
		
/** 题目:Magical GCD UVA 1642 链接:https://vjudge.net/problem/UVA-1642 题意:给定n个数,求使连续的一段序列的所有数的最大公约数*数的数量 ...
 - 求最大连续和——dp
		
输入一组整数,求出这组数字子序列和中最大值.也就是仅仅要求出最大子序列的和,不必求出最大的那个序列. 比如: 序列:-2 11 -4 13 -5 -2,则最大子序列和为20. 序列:-6 2 4 -7 ...
 - SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值
		
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
 - [Leetcode 216]求给定和的数集合 Combination Sum III
		
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
 - 我的第一篇博客:不用sizeof求int的bit数
		
我的第一篇博客.. 还不会什么高端的东西就来点基础的. 不用sizeof求int的bit数 //不用sizeof求int的bit数 #include<stdio.h> int main( ...
 - hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。
		
题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有 ...
 - HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
		
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
 
随机推荐
- Junit的简单使用
			
Junit是一个很好用的单元测试工具,下面是使用Junit来测试方法的简单案例: import java.util.ArrayList; import java.util.Iterator; impo ...
 - Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
			
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
 - 【Android开发日记】Popupwindow 完美demo
			
Popupwindow 完美demo实现 图示: 关键代码说明: 1.弹出popupwindow,背景变暗 ColorDrawable cd = new ColorDrawable(0x000000) ...
 - ASP.NET 取消和禁用缓存
			
客户端取消: <html> <head> <meta http-equiv="Expires" CONTENT="0"> & ...
 - Android HttpClient post MultipartEntity - Android 上传文件
			
转自[http://blog.csdn.net/hellohaifei/article/details/9707089] 在Android 中使用HttpClient,MultipartEntity ...
 - HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
			
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
 - Shell 编程基础之括号的作用
			
一.小括号() 单小括号 命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有分号,各命令和括号之间不必 ...
 - ural 2070. Interesting Numbers
			
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
 - CF#335 Board Game
			
Board Game time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...
 - 如何查看项目svn路径
			
1.选择项目根目录---->鼠标右键---->属性---->版本控制(Subversion) 如图: