正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
数据范围:
# 哭辽,不知道辗转相除法在那一直打转,真是拉胯呀,搞了好几个小时😭,期间模拟了几组数据发现最小公倍数=两者积/最大公约数,然后真给猜对了哈哈(题目标签是简单,所以愣是想着拒绝看题解或讨论或百度,务必要自己试着做出来) import sys import math def rec(n, count): if n % 2 != 0: return (count, n) else: count +=1 return rec(n>>1,count) def rec2(n1, n2, d): # if d > max(n1, n2):return 0 # 这里会超过递归的层数限制(1000左右),所以换一种思路: # 如果两个数能被某个小公约数整除,只需要参考更小的那个数 # 然而对于这个数来说,小公约数不可能超过这个数的平方根 if d > int(math.sqrt(min(n1, n2))) + 1:return 0 if n1 % d == 0 and n2 % d ==0: return d else: return rec2(n1, n2, d+2) def rec3(n, o, count): if n % o != 0: return count else: count +=1 return rec3(n/o, o, count) for line in sys.stdin: a = line.split() # print(a) max_ = max(int(a[0]), int(a[1])) min_ = min(int(a[0]), int(a[1])) if max_ % min_ == 0: print(max_) elif max_ % 2 ==0 and min_ % 2 == 0: # 8 12 max_c = rec(max_, 0)[0] min_c = rec(min_, 0)[0] print(int(max_ * min_ / (2 ** min(max_c, min_c)))) else: # 27 45/36 min_div = rec2(max_, min_, 3) if min_div == 0: print(max_ * min_) else: max_c = rec3(max_, min_div, 0) min_c = rec3(min_, min_div, 0) print(int(max_ * min_ / (min_div ** min(max_c, min_c)))) # else: # 27 12 # if max_ % 2 ==0: # factor = rec(max_, 0)[1]
def main(): a = input("") b = len(a) c = 0 while c < b - 1: if a[c] == " ": break c += 1 d = 0 e = "" while d < c: e += a[d] d += 1 f = c + 1 g = "" while f < len(a): g += a[f] f += 1 e = int(e) g = int(g) h = min(e, g) j = max(e, g) k = 2 while k <= h: if h % k == 0 and j % k == 0: h = h / k if h % k != 0: k += 1 else: k += 1 t = int(h * j) print(t) main()
#用递归方式模拟短除法 def back(num1:int,num2:int,res=1): m = min(num1,num2) if num1%num2 == 0&nbs***bsp;num2%num1==0: return max(num1,num2) else: for i in range(2,m+1): if num1%i==0 and num2%i==0: return back(num1//i,num2//i,res*i) return num1*num2*res a,b = map(int,input().split()) print(back(a,b))