51 nod 1012 最小公倍数LCM【数论】

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 收藏
 关注
输入2个正整数A,B,求A与B的最小公倍数。
 
Input
2个数A,B,中间用空格隔开。(1<= A,B <= 10^9)
Output
输出A与B的最小公倍数。
Input示例
30 105
Output示例
210

自己之前写的代码太丑了。数学方面的东西,自己还是需要多加练习。
#include<stdio.h>
#define LL long long 

LL gcd(LL a,LL b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
    return (a/gcd(a,b)*b);//如果是int,这样处理可以防止溢出
}
int main()
{
    LL n,m;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
        printf("%lld
",lcm(n,m));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hellocheng/p/7457112.html