`
judeylove
  • 浏览: 16735 次
  • 性别: Icon_minigender_1
  • 来自: 济南
最近访客 更多访客>>
社区版块
存档分类
最新评论

欧几里德算法的JAVA实现....好像网上搜索到的都不知道是什么东西

阅读更多
java 代码
  1. public class gdcTest {   
  2.        
  3.     /** Creates a new instance of gdcTest */  
  4.     public static void main(String[] args) {   
  5.        int a=125;   
  6.        int b=75;   
  7.        if(a < b){  int temp=a;   
  8.          a=b;   
  9.          b=temp;   
  10.         }   
  11.            
  12.        while(b!=0){   
  13.         int r=a%b;   
  14.         a=b;   
  15.         b=r;}   
  16.         System.out.println(a);   
  17.            
  18.        
  19.     }   
  20.     }  

网上看到过一个版本,而且传得很广,求两个数的最大公约数有那么复杂吗?

分享到:
评论
1 楼 judeylove 2007-09-19  
今天又研究了一个递歸的版本...
public class gdcTest {
    
    /** Creates a new instance of gdcTest */
    public static void main(String[] args) {
      
        System.out.println(gdc(a,b));
    
    }
    public static int gdc(int a,int b){
        if(b>a){
            int temp=a;
                 a=b;
                 b=temp;
        }
        if(b==0)return a;
        int temp2=a%b;
                a=b;
                b=temp2;
        return gdc(a,b);
    }
    private  static int a=7;
    private  static int b=2;
    }



相关推荐

Global site tag (gtag.js) - Google Analytics