首页 > 代码库 > 欧几里得算法——求取最小公约数

欧几里得算法——求取最小公约数

 1 import java.util.Scanner; 2  3 /** 4  * Created by Administrator on 14-5-20. 5  */ 6 public class Euclid { 7     public static void main(String[] args){ 8         Scanner scanner=new Scanner(System.in); 9         String str=scanner.nextLine();10         int a=Integer.parseInt(str);11         str=scanner.nextLine();12         int b=Integer.parseInt(str);13         System.out.println(gcd(a>b?a:b,a<=b?a:b));14     }15     public static  int gcd(int a,int b){16         int r;17         while(b!=0){18             r=a%b;19             a=b;20             b=r;21         }22         return a;23     }24 }