heibingtai

 

 

问题描述:

计算两个非负整数的最大公约数(the greatest common divisor) 

 

算法实现:

public static int gcd(int p, int q) {

if(q == 0) {
return p;
}

int r = p % q;
return gcd(q, r);
}

算法解析:

对于p和q,若q为0, 则最大公约数为p。

否则,将p除以q得到余数“r”,p和q的最大公约数即为q和“r”的最大公约数。

通过递归,直到“r”为0,得到最大公约数。

 


分类:

技术点:

相关文章:

  • 2021-12-19
  • 2021-09-07
  • 2021-12-21
  • 2021-09-14
  • 2021-10-16
  • 2019-09-03
  • 2019-07-27
  • 2021-10-03
猜你喜欢
  • 2021-12-09
  • 2021-05-06
  • 2021-11-01
  • 2021-05-02
  • 2021-09-17
  • 2021-11-27
  • 2021-05-02
相关资源
相似解决方案