m = int(input("请输入m的值:")) n = int(input("请输入n的值:")) max = 0 min = 0 if m > n: t = m m = n n = t for i in range(1, m+1): if m % i == 0 and n % i == 0: max = i min = m * n / max print("最大公约数为:{0},最小公倍数为:{1}".format(int(max), int(min)))
"""该函数返回两个数的最大公约数"""
def hcf(x, y):
# 获取最小值
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
"""该函数返回两个数的最小公倍数"""
def lcm(x, y):
# 获取最大的数
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
# 用户输入两个数字
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
print("最大公约数为",hcf(num1, num2),"最小公倍数为",lcm(num1,num2))
运行结果
望采纳 谢谢