计算两个数的最大公约数和最小公倍数(欧几里得算法)

利用欧几里得算法(即辗转相除法)计算两个整数的最大公约数

#include<iostream>
#include<algorithm>
using namespace std;
int gcb(int a,int b)
{
    if(b==0)
        return a;
    else
        return gcb(b,a%b);
}
int main()
{
    int a,b;
    int p,q;
    while(cin>>a>>b)
    {
        p = gcb(a,b); 
        q = a/p*b;    //最小公倍数,为防止溢出,应该先除后乘
        cout<<p<<" "<<q<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ttzz/p/10567030.html