【hackerrank】-Day 10: Binary Numbers

24 篇文章 0 订阅
24 篇文章 0 订阅

问题没有解决,有2个测试case失败了。待解决~~~~ 已解决!详见下面的方法!

30-binary-numbers
Objective
Today, we’re working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given a base- integer, , convert it to binary (base-). Then find and print the base- integer denoting the maximum number of consecutive ‘s in ‘s binary representation.

Input Format

A single integer, .

Constraints

Output Format

Print a single base- integer denoting the maximum number of consecutive ‘s in the binary representation of .

Sample Input 1

5
Sample Output 1

1
Sample Input 2

13
Sample Output 2

2
Explanation

Sample Case 1:
The binary representation of is , so the maximum number of consecutive ‘s is .

Sample Case 2:
The binary representation of is , so the maximum number of consecutive ‘s is .


Current Buffer (saved locally, editable)

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(input())
    sum = 0
    if n < 1 or n > 10**6:
        print('n < 1 or n > 10**6')
    binary_str = str(bin(n))[2:]
    check_list = []
    for i in range(len(binary_str)):
        if binary_str[i] == "1":
            sum += 1
            if i != len(binary_str)-1:
                continue
        # print("sum = %s" % sum)
        check_list.append(sum)
        sum = 0
    # print(check_list)
    check_list.sort(reverse=True)
    print(check_list[0])
''' # 下面的方法有问题,写的就是一坨啊!
    n = int(input())
    sum = 0
    if n < 1 or n > 10**6:
        print('n < 1 or n > 10**6')
    binary_str = str(bin(n))[2:]
    # print(binary_str)
    check_list = [0]
    jj = 0
    for i in range(len(binary_str)):
        if binary_str[i] == "1":
            jj = jj + 1
            # print(jj)
            continue
        # print(jj)
        if jj == 0:
            jj = 1
        if jj not in check_list:
            check_list.append(jj)
        jj = 0
    # print(check_list)
    check_list.sort(reverse=True)
    print(check_list[0])
'''

2 test cases failed :(

Ask your friends for help:
Testcase 0 Testcase 1 Testcase 2 Testcase 3
9 Testcases
Input (stdin)Download
439
Expected OutputDownload
3
Compiler Message
Wrong Answer

Input (stdin)Download
65535
Expected OutputDownload
16
Compiler Message
Wrong Answer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值