用函数求任意两个正整数的最小公倍数(LCM)。
#include <stdio.h>
int gcd(int a,int b){
return b!=0 ? gcd(b,a%b):a;
}
int main(){
int a,b;
scanf("%d,%d",&a,&b);
if(a<0||b<0)
printf("Input error!");
else
printf("LCM=%d",a*b/gcd(a,b));
return 0;
}
该程序实现了计算两个正整数最小公倍数(LCM)的功能,通过欧几里得算法计算最大公约数(GCD),然后利用公式LCM = a * b / GCD(a, b)得出结果。输入错误检查确保了输入为正整数。
3987

被折叠的 条评论
为什么被折叠?



