马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 枫枫duck 于 2020-4-9 10:38 编辑 def gcd(x,y):
big = x if x>y else y
small = y if x>y else x
while 1:
num = big%small
if num == 0:
return small
break
else:
big = small
small = big%small [code]
以上是我的编码,它会报错
[code]gcd(4,6)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
gcd(4,6)
File "C:\Users\15860\Desktop\python学习\lesson17函数\欧几里得.py", line 5, in gcd
num = big%small
ZeroDivisionError: integer division or modulo by zero
是说除数不能为0吗,为什么错了呢
本帖最后由 liuzhengyuan 于 2020-4-9 10:51 编辑
这个不对。。。
你这个
else:
big = small
small = big%small
想一想,
你先把big赋值small
这样big和small就一样了
两个一样的数相除余数就是0
|