//求最大公约数
publicstaticintcommonDivisor(intn,intm){
//辗转相除是用大的除以小的。如果nwhile(n%m!=0){
inttemp=n%m
n=m
m=temp
}
returnm
}
//求最小公倍数
publicstaticintcommonMultiple(intn,intm){
returnn*m/commonDivisor(n,m)//两数相乘除以最大公约数
}
你好,这是代码
LeastCommonMultipleTest.java
public class LeastCommonMultipleTest {public static void main(String[] args) {
// 三个数
int a = 7, b = 9, c = 11
// 最小公倍数
int number = 0
// 从0开始判断,无上限(最大可能是三个数字的乘积)
while (!isCommonMultipleNum(number, new int[] { a, b, c })) {
// 从0开始判断,不是公倍数就+1
number++
}
System.out.printf("最小公倍数是:" + number)
}
/**
* 判断某数是否为几个数字的公倍数
*
* @param number
* 要判断是否为公倍数的数字
* @param arr
* 数字数组
* @return 判断结果,是公倍数返回true,不是返回false
*/
private static boolean isCommonMultipleNum(int num, int[] arr) {
// 循环判断每一个数字
for (int i = 0 i < arr.length i++) {
int current = arr[i]
if (num < current || num % current != 0) {
return false
}
}
return true
}
}
import java.util.*publicclass lianxi06 {
public static void main(String[] args) {
int a ,b,m
Scanner s = new Scanner(System.in)
System.out.print( "键入一个整数: ")
a = s.nextInt()
System.out.print( "再键入一个整数: ")
b = s.nextInt()
deff cd = new deff()
m = cd.deff(a,b)
int n = a * b / m
System.out.println("最大公约数: " + m)
System.out.println("最小公倍数: " + n)
}
}
class deff{
public int deff(int x, int y) {
int t
if(x <y) {
t = x
x = y
y = t
}
while(y != 0) {
if(x == y) return x
else {
int k = x % y
x = y
y = k
}
}
return x
}
}