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

#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;
}

 

posted on 2019-03-20 19:22  曹婷婷  阅读(1028)  评论(0)    收藏  举报