我被赋予了计算任意两个数字的lcm的任务。我在python.The问题中进行了编码,当我在python2.7下编译它时,我得到了一个与在python3下编译时不同的结果。
import sys
def gcd(a,b):
if b == 0:
return a
remainder = a % b
return gcd(b,remainder)
def lcm(a, b):
return int( (a*b) / gcd(a,b))
if __name__ == '__main__':
input = sys.stdin.read()
a, b = map(int, input.split())
print(int(lcm(a, b)))输入
226553150 1023473145
输出
46374212988031352 (低于丙酮3.5) 46374212988031350 (低于python2.7)
有人能帮我吗?
发布于 2016-06-14 13:46:00
在Python2中,除法操作符/在处理两个整数时将执行整数除法,而不是浮点数除法。您可以通过从__future__导入除法来强制Python2中的Python3行为
>>> from __future__ import division
>>> 2/3
1.5https://stackoverflow.com/questions/37813586
复制相似问题