HR30 Day 10: Binary Numbers

1 개요[ | ]

Day 10: Binary Numbers
해커랭크 30 Days of Code
문제 풀이
10-19 Day e
HR30 Day 10: Binary Numbers

HR30 Day 11: 2D Arrays

HR30 Day 12: Inheritance

HR30 Day 13: Abstract Classes

HR30 Day 14: Scope

HR30 Day 15: Linked List

HR30 Day 16: Exceptions - String to Integer

HR30 Day 17: More Exceptions

HR30 Day 18: Queues and Stacks

HR30 Day 19: Interfaces

2 Java[ | ]

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        scanner.close();
        
        int cnt = 0, max = 0;
        while( n > 0 ) {
            if( n%2 == 1 ) {
                cnt++;
                if( cnt > max ) max = cnt;
            }
            else cnt = 0;
            n /= 2;
        }
        System.out.println( max );
    }
}

3 PHP[ | ]

<?php
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
fclose($stdin);
$b = decbin($n);
$max = 0;
$cnt = 0;
for($i=0; $i<strlen($b);$i++) {
    if($b[$i] == 1 ) {
        $cnt++;
        if( $cnt > $max ) $max = $cnt;
        continue;
    }
    $cnt = 0;
}
echo $max;

4 Python[ | ]

#!/bin/python3
import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(input())
    cnt = 0
    mx = 0
    while n > 0:
        if n%2 == 1:
            cnt += 1
            if cnt > mx: mx = cnt
        else: cnt = 0
        n //= 2
    print( mx )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}