|

楼主 |
发表于 2021-3-3 11:38:47
|
显示全部楼层
/**
* 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
*/
public class Subject6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要求最大公约数和最小公倍数的两个数字");
System.out.println("请输入第一个数字m:");
int a = scanner.nextInt();
System.out.println("请输入第二个数字n:");
int b = scanner.nextInt();
//求最小公倍数
System.out.println("最小公倍数为:" + a * b / greartestCommonDivisor(a, b));
}
/**
* 求m和n的最大公约数:
* 辗转相除法求最大公约数:›
*
* @return
*/
public static int greartestCommonDivisor(int m, int n) {
if (m > n) {
if (m % n == 0) {
System.out.println("最大公约数为:" + n);
} else {
greartestCommonDivisor(n, m % n);
}
} else {
greartestCommonDivisor(n, m);
}
return n;
}
} |
|