用递归求三个数的最大公约数,,,好难啊,,怎么求,,我新手啊,,想了半天
老班的题目啊: 声明求最大公约数的递归方法,写出求两个整数a,b的最小公倍数、三个整数最大公约数的调用语句。
public class GCD_LCM { public static void main(String[] args) { int a = 0; int b = 0; int c = 0; a = 1; b = 2; c = 3; System.out.println(a + " " + b + " " + c + "'s gcd:" + gcd(a, b, c)); System.out.println(a + " " + b + "'s lcm:" + lcm(a, b)); a = 10; b = 20; c = 30; System.out.println(a + " " + b + " " + c + "'s gcd:" + gcd(a, b, c)); System.out.println(a + " " + b + "'s lcm:" + lcm(a, b)); } public static int gcd(int a, int b, int c) { // Greatest Common Divisor return gcd(gcd(a, b), c); } public static int gcd(int a, int b) { // Greatest Common Divisor if (a == 0) { return b; } else { while (b != 0) { if (a > b) { a = a - b; } else { b = b - a; } } return a; } } public static int lcm(int a, int b) { // Least Common Multiple return a * b / gcd(a, b); } }