python 本题要求两个给定正整数的最大公约数和最小公倍数

python  本题要求两个给定正整数的最大公约数和最小公倍数

问题描述:

7-11 本题要求两个给定正整数的最大公约数和最小公倍数。 (15 分)

本题要求两个给定正整数的最大公约数和最小公倍数。

输入格式:

输入在两行中分别输入正整数x和y。

输出格式:

在一行中输出最大公约数和最小公倍数的值。

输入样例1:

在这里给出一组输入。例如:

100
1520

输出样例1:

在这里给出相应的输出。例如:

20 7600

"""该函数返回两个数的最大公约数""" 
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))

运行结果: