最大公约数(Greatest common divisor 和 最小公倍数(least common multiple)

#include<bits/stdc++.h>
using namespace std;

int gcd(int a,int b){//最大公约数
    return b==0?a:gcd(b,a%b);
}

int lcm(int a,int b){//最小公倍数,最小公倍数等于两数的乘积除最大公约数
    return a*b/gcd(a,b);
}

int main(){
    int a=18;
    int b=24;
    cout<<gcd(a,b)<<endl;
    cout<<lcm(a,b)<<endl;
    //输出:6 72

}

 

posted @ 2019-03-17 15:39  开局一把刀  阅读(2)  评论(0)    收藏  举报