Java求最大公约数

来源:互联网 发布:点子网络 编辑:程序博客网 时间:2024/05/16 14:40

辗转相除法(递归版本)

摘录自网上

  1. int commonDivisor(int M, int N)
  2.     {
  3.         if(N<0||M<0)
  4.         {
  5.             System.out.println("ERROR!");
  6.             return -1;
  7.         }
  8.         if(N==0)
  9.         {
  10.             System.out.println("the biggest common divisor is :"+M);
  11.             return M;
  12.         }
  13.         return commonDivisor(N,M%N);
  14.     }
原创粉丝点击