zoukankan      html  css  js  c++  java
  • 最大公约数 最小公倍数

    #include <stdlib.h>
    #include <math.h>
    #include <iostream>
    using namespace std;
    
    int greatest_common_divisor1(int a,int b){ //辗转相减法 
        while(a!=b){
            if(a>b) a=a-b;
            else b=b-a;
        }
        return a; 
    }
    int greatest_common_divisor2(int a,int b){//辗转相除法 
        int c;
        while(b!=0){
            c=a%b;
            a=b;
            b=c;
        }
        return a;
    }
    int main(){
        int a,b,m,l;
        cin>>a>>b;
        l=greatest_common_divisor2(a,b);
        m=greatest_common_divisor1(a,b);
        cout<<"最大公约数:(辗转相减法)"<<m<<endl;
        cout<<"最大公约数:(辗转相除法)"<<l<<endl;
        cout<<"最小公倍数:"<<a*b/m;
        return 0;
    } 
  • 相关阅读:
    四种wordpress常用的循环结构
    自动创建网页文章目录结构
    shell
    SSH
    架构
    Https
    if-else、switch、while、for
    do-while
    const
    Tail Call
  • 原文地址:https://www.cnblogs.com/xusi/p/14269118.html
Copyright © 2011-2022 走看看