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-08-05
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2021-11-01
  • 2022-01-15
  • 2022-12-23
猜你喜欢
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-06
  • 2021-09-08
相关资源
相似解决方案