• 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

     1 <= n <= 10^6

    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.

     

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int main(){
     5     int n;
     6     cin >> n;
     7     int temp = n;
     8     int num = 0, max = 0;
     9     while(temp){
    10         num = 0;
    11         while(temp & 1){
    12             num++;
    13             temp >>= 1;
    14         }
    15         if(num > max)
    16             max = num;
    17         temp >>= 1;
    18     }
    19     cout << max << endl;
    20     return 0;
    21 }
    View Code

     

     

  • 相关阅读:
    GlusterFS 配置及使用
    zabbix-监控Linux服务器
    ansible安装及使用
    使用ansible 完成yum安装lamp环境
    mysql基础
    shell基础
    shell
    Javascript动画效果(四)
    Javascript动画效果(三)
    Javascript动画效果(二)
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5531552.html
Copyright © 2020-2023  润新知