鱼C论坛

 找回密码
 立即注册
查看: 2154|回复: 2

编程题求助

[复制链接]
发表于 2016-12-20 12:20:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是CodeFights上面的一道编程题。

Implement a function that, given an integer n, will return the number of bits in its binary representation.
Example
For n = 50, the output should be
countBits(n) = 6.
5010 = 1100102, a number that consists of 6 digits. Thus, the output should be 6.
Input/Output
  • [time limit] 4000ms (py3)
  • [input] integer n
    A positive integer.
    Constraints:
    1 ≤ n ≤ 109.
  • [output] integer
    The number of bits in binary representation of n.


给出的代码:

def countBits(n):
    return n. ...()

只能修改...的内容,请问这道题要怎么做。

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-20 14:09:10 | 显示全部楼层
  1. >>> def countBits(n):
  2.         return n.bit_length()
  3. >>> countBits(50)
  4. 6
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-12-20 21:28:26 | 显示全部楼层
加油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-24 14:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表