public class JavaBase { static public int gcd1_1(int x, int y) //非递归的辗转相除法 { int temp; do{ temp = x % y; x = y; y = temp; }while(temp != 0); return x; } static public int gcd2_1(int x, int y) //非递归的辗转相减法 { int max, min; int temp; max = (x > y) ? x : y; min = (x < y) ? x : y; while (max != min) { temp = max - min; max = (temp > min) ? temp : min; min = (temp < min) ? temp : min; } return max; } static public int gcd1_2(int x, int y) //递归的辗转相除法 { return (y == 0) ? x : gcd1_2(y, x % y); } static public int gcd2_2(int x, int y) //递归的辗转相减法 { if(x == y) return x; return (x > y) ? gcd2_2(x - y, y) : gcd2_2(x, y - x); } public static void main(String args[]) { int a = 28, b = 48; int g = 0; g = gcd1_1(a, b); System.out.println("最大公约数为:" + g); g = gcd1_2(a, b); System.out.println("最大公约数为: " + g); g = gcd2_1(a, b); System.out.println("最大公约数为: " + g); g = gcd2_2(a, b); System.out.println("最大公约数为:" + g); System.out.println("最小公倍数为: " + a * b / g);//最小公倍数 }