Python--最大公约数

  今天敲出了求最大公约数的代码,并逐步进行了改进。开心!!!

   第一次的代码:

a=eval(input())
b=eval(input())
i=min(a,b)
for i in range(b,1,-1):
if a%i==0 and b%i==0:
print(i)
break
View Code


   第二次翻书发现,有一个可以直接求最大公约数的函数math.gcd(a,b),于是加以改进:

a=eval(input())
b=eval(input())
c=math.gcd(a,b)

print(c)
View Code

  第三次继续改进:

import math
print
(math.gcd(eval(input()),eval(input())))

哈哈哈!!!

猜你喜欢

转载自www.cnblogs.com/khqhbu/p/10680686.html