递归算法-求最大公约数-java实现

来源:互联网 发布:mac mobi convert 编辑:程序博客网 时间:2024/06/05 17:49
  1. /**
  2. * 递归算法:求最大公约数,根据欧几里德知道-》m和n(m > n)的最大公约数 = n 和m%n的最大公约数
  3. *
  4. * @author timmy1
  5. *
  6. */
  7. public class GreatestCommonDivisor {
  8. public int getGCD(int m, int n) {
  9. if (n == 0) {
  10. return m;
  11. } else {
  12. return getGCD(n, m % n);
  13. }
  14. }
  15. public static void main(String[] args) {
  16. GreatestCommonDivisor gcd = new GreatestCommonDivisor();
  17. int div = gcd.getGCD(90, 60);
  18. System.out.println("最大公约数为:" + div);
  19. }
  20. }

0 0
原创粉丝点击