码迷,mamicode.com
首页 > 其他好文 > 详细

【数论】辗转相除法

时间:2016-11-14 19:53:00      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:turn   题目   最大公约数   splay   .com   style   img   ace   contract   

AOJ 0005 题目是英文的,我就不具体翻译了。就是求最大公约数和最小公倍数。

 

(补充下 设两个数是a,b最大公约数是p,最小公倍数是q
那么有这样的关系:ab=pq
所以q=ab/p)

技术分享
#include<iostream>
#include<cstdio>
using namespace std;

int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
} 

int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}

int main()
{
    int a,b;
    while(cin>>a>>b) {
        cout<<gcd(a,b)<<" "<<lcm(a,b)<<endl;
    }
    return 0;
}
GCD和LCM

 

POJ 2429 挖坑待续

POJ 1930 挖坑待续

【数论】辗转相除法

标签:turn   题目   最大公约数   splay   .com   style   img   ace   contract   

原文地址:http://www.cnblogs.com/OIerLYF/p/6062949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!