python - 不使用 if-else 实现 ceil 函数

标签 python algorithm ceil

我只是想知道有没有一种方法可以不使用 if-else 来实现 ceil 函数? 使用 if-else (for a/b) 可以实现为:

if a%b == 0:
    return(a/b)
else:
    return(a//b + 1)

最佳答案

最简单的是。

a//b + bool(a%b)

为了安全,

b and (a//b + bool(a%b))

干杯。

关于python - 不使用 if-else 实现 ceil 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140332/

相关文章:

python - Azure存储: Blob: Python: Get indicator if there are blobs at all

python - 范围 Pandas 之间字典中的查找值

if 和 elif 之间的 Python 变量

c# - 给定一个整数数组。找到具有最大总和的最大子数组

Python 四舍五入到 10 的下一个最高幂

javascript - 从 Python 上的 post 方法获取变量

java - 递归前序遍历算法如何回到parent?

c++ - 二叉树中的最小路径和

java - 为什么 math.pow 本身不能处理整数? (地板/天花板也是)

c - 了解 CEILING 宏用例