如果两数相等,则返回其中一个
如果两数不等,先用大数large除以小数small取余数(remainder),此时可能有两种情况:
- small 不能被 remainder 整除。此时最小的能同时被 large,small 整除的整数是:
large * remainder - small 能被 remainder 整除,此时最小的能同时被 large,small 整除的整数是:
large * (remainder与small的最小公倍数 / remainder)
举例:
5,7
small=5, large=7, remainder=2
small 不能被 remainder 整除,他俩最小公倍数是10
所以答案是 large * (remainder与small的最小公倍数/remainder) = 7 * (10 / 2) = 35
又例:
10,4
small=4, large=10, remainder=2
small 能被 remainder 整除,他俩最小公倍数是4
所以答案是 large * (remainder与small的最小公倍数/remainder) = 10 * (4 / 2) = 20
class Solution():
def leastCommonMultiple(self, a:int, b:int) -> int:
large = a if a>b else b
small = a if a<b else b
remainder = large%small
if remainder==0:
return large
else:
return int(self.leastCommonMultiple(small,remainder)/remainder*large)
str1 = input().split(' ')
a,b = int(str1[0]),int(str1[1])
s = Solution()
print(s.leastCommonMultiple(a,b))