int函数及其参数

2024-06-16 08:49:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理this series of problems。在

我不明白问题11和使用int()的方式。我知道将x直接转换为int会导致一些零的丢失,所以这样做是不可能的,但我不明白是什么

intp = int(p,2) 

是应该做的。打印出intp我得到以下值,4,3,10,9。这与0100,0011,1010,1001有什么关系?为什么一开始就丢失了零?在

问题11
2级

Question: Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.

Example:

0100,0011,1010,1001

Then the output should be:

1010

Notes: Assume the data is input by console.

Hints: In case of input data being supplied to the question, it should be assumed to be a console input.

^{pr2}$

Tags: ofthetoinputbybeareint
3条回答
value = []
items=[x for x in input().split(',')]
for p in items:
    intp = int(p, 2)
    value.append(intp)

for x in value:
    if x%5==0:
        print(bin(x))

要将二进制数转换为十进制数,我们必须使用python内置的int函数,该函数以二进制数和数字系统的基为参数。 示例:

>>> p='1010'
>>> c=int(p,2)
>>> print c
    10

正如上面的grumpy注释所建议的,int函数接受第二个参数,这是转换的基础。所以它取绑定到p的base2(二进制)变量,并将其转换为以10为基数的整数,也就是说,普通人。在

相关问题 更多 >