最大公约数和最小公约数

#include<iostream>

using namespace std;

void main()

{

int x,y,a,b,m=1;

cout<<"please input the number:"<<endl;

cin>>x>>y;

if(x>y)

{

a=x;b=y;

}

else

{

a=y;b=x;

}

do

{

m=a%b;

a=b;

b=m;

}while(m!=0);

cout<<"公约数:"<<a<<endl;

cout<<"公倍数:"<<x/a*y<<endl;

}



//第二种算法:

#include<iostream>

using namespace std;

int shu(int &x,int &y)

{

int m;

if(x>y)

{

m=y;

}

else 

{

m=x;

}

while (x%m !=0 || y%m!=0)

{

m--;

}

return m;

}

int mu(int &x,int &y)

{  

int n;

if(x>y)

{

n=x;

}

else 

{

n=y;

}

while (n%x!=0 || n%y!=0)

{

n++;

}

return n;

}

void main()

{

int x,y,p,q;

cout<<"please input the number:"<<endl;

cin>>x>>y;

p=shu(x,y);

q=mu(x,y);

cout<<"公约数:"<<p<<endl;

cout<<"公倍数:"<<q<<endl;



}

  

你可能感兴趣的:(最大公约数和最小公约数)