[c]代码库
int gcd(int a, int b)//最大公约数
{
int i, div = 1;
for(i = 1; i<=a/2 && i<=b/2;i++){//小的结束即为结束
if(a%i==0 && b%i==0){//共同的约数
div = i;
}
}
return div;
}
int lcm(int a, int b){//最小公倍数
int i, j, m1=a, m2=b;
i = 2;
j = 2;
if(a>b && a%b==0)//可以整除的话,大的为最小公倍数
return a;
if(a<b && b%a==0)
return b;
while(m1 != m2){ //从2开始乘原数,比较大小,小的将乘数i加一,大的不变,直到相等。
if(m1 < m2){
m1 = a*i;
i++;
}
else{
m2 = b*j;
j++;
}
printf("%d %d\n", m1, m2);
}
return m1;
}
int main(){
int gcd(int a, int b);
int lcm(int a, int b);
int a, b, div, mul, c, d;
scanf("%d%d", &a, &b);
scanf("%d%d", &c, &d);
div = gcd(a, b);
mul = lcm(c, d);
printf("greatest common divisor:%d\n", div);
printf("least common multiple:%d\n", mul);
return 0;
}