Java 中数组数字的最小公倍数
两个值的最小公倍数,是同时包含这两个值的最小正值。
例如,3 和 4 的倍数为
3 → 3、6、9、12、15 ...
4 → 4、8、12、16、20 ...
同时包含这两个值的最小倍数是 12,因此 3 和 4 的最小公倍数为 12。
程序
以下示例计算数组数字的最小公倍数。
public class LCMofArrayOfNumbers {
public static void main(String args[]) {
int[] myArray = {25, 50, 125, 625};
int min, max, x, lcm = 0;
for(int i = 0; i<myArray.length; i++) {
for(int j = i+1; j<myArray.length-1; j++) {
if(myArray[i] > myArray[j]) {
min = myArray[j];
max = myArray[i];
} else {
min = myArray[i];
max = myArray[j];
}
for(int k =0; k<myArray.length; k++) {
x = k * max;
if(x % min == 0) {
lcm = x ;
}
}
}
}
System.out.println("LCM of the given array of numbers : " + lcm);
}
}输出
LCM of the given array of numbers : 250
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP