在求两个数的最大公约数方法中,

辗转相除法是比较快的一种方法。

也就是著名的欧几里德方法。

View Code
int Gcd(int a, int b){
   return b==0?a:gcd(b, a%b);
}

 

 

View Code
#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
#include "algorithm"
using namespace std;

int Gcd(int a, int b)//殴几里得算法,求最大公约数
{
if(b==0) return a;
else return Gcd(b, a%b);
}

int main()
{
int x, y;
while(cin>>x>>y)
{
cout<<Gcd(x, y)<<endl;
}
}



相关文章:

  • 2022-01-27
  • 2021-11-24
  • 2022-01-30
  • 2021-05-06
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-04
  • 2022-01-22
  • 2022-12-23
  • 2021-10-28
  • 2021-06-19
  • 2022-12-23
相关资源
相似解决方案