题解 | #求int型正整数在内存中存储时1的个数#

求int型正整数在内存中存储时1的个数

http://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9

num = int(input()) #接受输入
bina = bin(num)    #转换二进制
str1 = str(bina)     #转二进制字符串
count = 0
#计算1出现的次数
for i in str1:          
    if i == '1':
        count +=1
print(count)
全部评论
print(str1.count('1'))和这几行代码功能一样: count = 0 #计算1出现的次数 for i in str1: if i == '1': count +=1 print(count)
点赞 回复 分享
发布于 2023-07-28 14:20 陕西

相关推荐

评论
16
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务