题解 | 求最小公倍数

求最小公倍数

https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3

import sys

for line in sys.stdin:
    a = line.split("\n")[0].split(" ")
    a0,a1 = int(a[0]),int(a[1])
    if a0 % a1 ==0 or a1 % a0==0:
        print(max(a0,a1))
    else:
        temp = a0*a1
        if temp %2 == 0:
            while temp/2 % a0 == 0 and temp/2 % a1 == 0:
                temp = temp/2
            print(int(temp))
        elif temp %3 == 0:
            while temp/3 % a0 == 0 and temp/3 % a1 == 0:
                temp = temp/3
            print(int(temp))
        else:
            print(temp)

如果最大的那个能直接当作最下公倍数

那么就能直接用

但是如果不行,那么就先相乘

然后不断%2 %3 直到最小为止,那么最小的那个就是目标

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务