# wrong in Python 3; works with Python 2.3 to 2.7
# int overflow with Pythons up to 2.2
>>> int((100000000000000000 + 2)/3)
33333333333333332 # last digit should be 4
# wrong with Python 2.3 onwards; int overflow with earlier versions
>>> import math
>>> int(math.ceil(float(100000000000000000) / 3))
33333333333333332L
在Python中,使用
//
(integer floor division,在python2.2中引入)并强制它向上取整:或者如果您需要所谓的“专业程序员”版本而不是解释性版本:
或者如果你真的害怕腕管综合症,切掉两个多余的括号和分号:
注意:这个解决方案同时适用于python2(其中
10 / 3 -> 3
)和python3(其中10 / 3 -> 3.3333333333333335
)其他的“解决方案”不仅使用不必要的函数调用,而且也会因大量数据而失败:
听起来你想要算术:
如果您有可能得到剩余的情况:
相关问题 更多 >
编程相关推荐