【华为OJ】【084-求最大连续bit数】
2016-05-19 06:43
381 查看
【华为OJ】【算法总篇章】
【华为OJ】【084-求最大连续bit数】
【工程下载】
题目描述
功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 无 返回: 对应的二进制数字中1的最大连续数
输入描述
输入一个byte数字
输出描述
输出转成二进制之后连续1的个数
输入例子
3
输出例子
2
算法实现
import java.util.Scanner; /** * Author: 王俊超 * Date: 2016-01-04 09:44 * Declaration: All Rights Reserved !!! */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt")); while (scanner.hasNext()) { int b = scanner.nextInt(); System.out.println(countBit(b)); } scanner.close(); } private static int countBit(int b) { int max = 0; int cur = 0; b &= 0xFF; for (int i = 0, and = 1; i < 8; i++) { // 如果第i位为1 if ((b & and) != 0) { cur++; if (cur > max) { max = cur; } } else { cur = 0; } and <<= 1; } return max; } }
相关文章推荐
- 编程思想之多线程与多进程(4)——C++中的多线程
- 【华为OJ】【083-计算字符串的相似度】
- 还需要补充很多知识
- LeetCode 19. Remove Nth Node From End of List(删除链表)
- (加精)AndrodStudio 极光推送 推送类手写版加精Demo 续写!!!
- leetcode 11 in c++
- 【华为OJ】【082-字符串通配符】
- LeetCode 18. 4Sum(四数之和)
- 船已开始颠簸,大家坐好
- 【华为OJ】【081-查找两个字符串a,b中的最长公共子串】
- 【Unity】13.3 Realtime GI示例
- 源码推荐(0519):PickerView,星星形状的评分进度条
- 【Unity】13.2 通过Lighting Window设置相关参数
- LeetCode 17. Letter Combinations of a Phone Number(键盘字母)
- Swagger - 前后端分离后的契约
- 【Unity】13.1 场景视图中的GI可视化
- 转载 C#中敏捷开发规范
- LeetCode 16. 3Sum Closest(最接近的和)
- 【Unity】第13章 光照贴图和光影效果
- iOS动画篇:自定义动画