辗转相除法的算法为:首先将 m除以 n(m>n)得余数 r,再用余数 r 去除原来的除数,得新的余数,重复此过程直到余数为 0时停止,此时的除数就是m 和 n的最大公约数。
public class GcdLcm {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 377, b = 319, c, d, e;
if (a > b) {
a = a + b;
b = a - b;
a = a - b;
}
d = a;
e = b;
c = a % b;
while (c != 0) {
c = a % b;
a = b;
b = c;
}
System.out.println("最大公约数是:" + a);
c = (d * e) / a;
System.out.println("最小公倍数:" + c);
}
}