hackerrank Day 10: Binary Numbers
Task
Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in n's binary representation.
Input Format
A single integer, n.
Constraints
Output Format
Print a single base-10 integer denoting the maximum number of consecutive 1's in the binary representation of n.
Sample Input 1
5
Sample Output 1
1
Sample Input 2
13
Sample Output 2
2
Explanation
Sample Case 1:
The binary representation of 5 is 101, so the maximum number of consecutive 1's is 1.
Sample Case 2:
The binary representation of 13 is 1101 , so the maximum number of consecutive 1's is 2.
#include <iostream>
using namespace std; int main(){
int n;
cin >> n;
int temp = n;
int num = , max = ;
while(temp){
num = ;
while(temp & ){
num++;
temp >>= ;
}
if(num > max)
max = num;
temp >>= ;
}
cout << max << endl;
return ;
}
hackerrank Day 10: Binary Numbers的更多相关文章
- HDU-1390 Binary Numbers
http://acm.hdu.edu.cn/showproblem.php?pid=1390 Binary Numbers Time Limit: 2000/1000 MS (Java/Others) ...
- Binary Numbers(HDU1390)
Binary Numbers 点我 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- zoj 1383 Binary Numbers
Binary Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB Given a positive integer n, print o ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...
- [Swift]LeetCode1022. 从根到叶的二进制数之和 | Sum of Root To Leaf Binary Numbers
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number ...
- Binary Numbers AND Sum CodeForces - 1066E (前缀和)
You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...
- 【leetcode】1022. Sum of Root To Leaf Binary Numbers
题目如下: Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary n ...
随机推荐
- uva 11995 I Can Guess the Data Structure stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- 【转载】C/C++语言void及void指针深层探索
C/C++语言void及void指针深层探索 1.概述许多初学者对C/C++语言中的void及void指针类型不甚理解,因此在使用上出现了一些错误.本文将对void关键字的深刻含义进行解说,并详述vo ...
- HW5.35
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- asp.net(C#) 中 怎么使用 MongoDb
1. 先引用以下Dll(如果找不到 到gethub上下载源代码自己编译 特别是MongoDB.Driver.Legacy.dll 我自己找了半天没找到): MongoDB.Bson.dll Mongo ...
- SQL2008-查询库中是否存在某表
select * from sysobjects where name= 'N201111B' and xtype= 'U'
- elecworks 电缆型号管理器
部件的命名:系列 导线数x尺寸 (不含保护类型的电线) 或:系列 导线数G尺寸 G(指保护线颜色gr ...
- nyoj 49 开心的小明
开心的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天 ...
- 在线性级别时间内找出无序序列中的第k个元素
在一个无序序列中找出第k个元素,对于k很小或者很大时可以采取特殊的方法,比如用堆排序来实现 .但是对于与序列长度N成正比的k来说,就不是一件容易的事了,可能最容易想到的就是先将无序序列排序再遍历即可找 ...
- Android短彩信源码解析-短信发送流程(三)
3.短信pdu的压缩与封装 相关文章: ------------------------------------------------------------- 1.短信发送上层逻辑 2.短信发送f ...
- MySQL中UNION和UNION ALL的使用
在数据库中,UNION和UNION ALL关键字都是将两个结果集合并为一个,但这两者从使用和效率上来说都有所不同. MySQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后 ...