问题没有解决,有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