11、求两个数的最大公约数和最小公倍数。

#include<iostream>
#include<math.h>
using namespace std;
int main(){
    int  a,b;
    cin>>a>>b;
    int  m=a;
    int  n=b;
    int c;
    if(a<b){
       int temp=a;
       a=b;
       b=temp;        
    } 
    while(b!=0){
       c=a%b;
       a=b;
       b=c;                  
    }
    cout<<"最大公约数"<<a<<endl;
    cout<<"最小公倍数"<<m*n/a<<endl;
    system("pause");
}

利用辗转相除法,来求两个数的最大公约数,然后用两个数的乘积,然后除以最大公约数,求得最小公倍数。

猜你喜欢

转载自blog.csdn.net/qq_30272539/article/details/81200565