【问题标题】:Getting repetitions of one in the base 2 representation of a number python在数字python的base 2表示中重复一个
【发布时间】:2022-07-04 16:12:11
【问题描述】:

我想在输入中输入一个数字,并在其二进制基础中获取一个的出现次数

print(str({int(input()):'b'}).count('1'))

我写的代码是这样的,但是例如:

input:3
output:0

错了

【问题讨论】:

  • str({int(input()):'b'}) 给你"{3:'b'}"。有一个bin 函数用于将数字转换为二进制。

标签: python python-3.x


【解决方案1】:

按以下步骤进行:

   print("{:b}".format(int(input())).count('1'))

input:3
output:2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2012-09-24
    • 2020-11-14
    • 2021-07-22
    • 2014-03-08
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多