当前位置:网站首页>3 find the greatest common divisor and the least common multiple

3 find the greatest common divisor and the least common multiple

2022-07-05 22:43:00 buyizhu021

Title Description

Enter two integers , Find the greatest common divisor and the least common multiple of two integers , And output the result .

Let two integers be u and v, The algorithm for finding the greatest common divisor and the least common multiple by rolling division is as follows :

if v>u

   Put the variable u And v In exchange for              ( Make the big one u For divisor )

while (u/v The remainder of r!=0)

   {u=v                              ( Divisor v Become a divisor u

     v=r                              ( Make the remainder r Become a divisor v

}

Output the greatest common divisor v( at present )

Minimum common multiple lu*v( original )/ greatest common divisor v

The sample input

36 12

Sample output

greatest common divisor :12
Minimum common multiple :36

Code

#include <iostream>
using namespace std;
int max(int m,int n)
{int t,r,max;
 if(n>m)
  {t=n;n=m;m=t;}
  r=m%n;
  while(r!=0)
  {m=n;n=r;r=m%n;}
  max=n;
  return max;
}
int min(int m,int n,int c)
{int min;
 min=m*n/c;
 return min;
}
int main()
{ int m,n,x,y;
  cin>>m>>n;
  x=max(m,n);
  y=min(m,n,x);
  cout<<" greatest common divisor :"<<x<<endl;
  cout<<" Minimum common multiple :"<<y<<endl;
  return 0;
}

原网站

版权声明
本文为[buyizhu021]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140406043921.html