public class Main {
public static void main(String[] args) {
int a = 12, b = 18;
int gcd = gcd(a, b);
int lcm = lcm(a, b);
System.out.println("最大公因数:" + gcd);
System.out.println("最小公倍数:" + lcm);
}
// 求最大公因数
public static int gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
// 求最小公倍数
public static int lcm(int a, int b) {
int gcd = gcd(a, b);
return a / gcd * b;
}
}
标签:gcd,公倍数,最小,int,lcm,public,公因数
From: https://www.cnblogs.com/chenyi502/p/17294002.html