在1-500间产生3个随机整数a,b,c,求他们的最大公约数和最小公倍数。
题是这样的不知道咋写了,教我一下谢谢
望采纳
import random
def gcd(a,b):
if a%b==0:
return b
return gcd(b,a%b)
def lcm(a,b):
return a//gcd(a,b)*b
a=random.randint(1,500)
b=random.randint(1,500)
c=random.randint(1,500)
print("a:",a,"b:",b,"c:",c,"\n最大公约数:",gcd(gcd(a,b),c),"最小公倍数:",lcm(lcm(a,b),c))
给个例子供参考:
ls = list(map(int, input("请输入3个1-500之间的整数,用','隔开:").split(',')))
ls.sort()
maxgys = 1
for i in range(2, ls[0]+1):
if ls[0]%i==0 and ls[1]%i==0 and ls[2]%i==0:
maxgys = i
print(f'{ls[0]},{ls[1]},{ls[2]}的最大公约数是:{maxgys}')
temp = 1
for i in range(2, ls[0]+1):
if ls[0]%i==0 and ls[1]%i==0:
temp = i
ab = int(ls[0]*ls[1]/temp)
temp = 1
for i in range(2, min(ab,ls[2])+1):
if ab%i==0 and ls[2]%i==0:
temp = i
mingbs = ab*ls[2]/temp
print(f'{ls[0]},{ls[1]},{ls[2]}的最小公倍数是:{mingbs}')
如有帮助,请采纳!