[cpp]
#include <stdio.h>
//獲得最小公倍數
int doLCM(int* array,int size){
int x,y,temp,gcd=array[0],i,result=1;
for(i=0;(i+1)<size;i++){
x=gcd;
y=array[i+1];
//保證x>y
if(x < y){
temp = y;
y = x;
x = temp;
}
gcd = getGCD(x,y);
}
//將每個數除以最大公約數,然後相乘
for(i=0;i<size;i++){
result *= array[i]/gcd;
}
//最後再乘以最大公約數
return result*gcd;
}
//獲取最大公約數
int getGCD(int a,int b){
if(a%b == 0){
return b;
}else{
return getGCD(b,a%b);
}
}
int main(void){
int array[3] = {144,24,36};
int result = doLCM(array,3);
printf("%d\n",result);
return 0;
}