目录
最小公倍数:
两个或多个整数公有的倍数叫做他们的公倍数————来源百度百科
例如:
【3和4】3的倍数有6、9、12、15、18……;4的倍数有4、8、12、16……
它们公有的倍数有12……,则3和4的最小公倍数为12。
代码:
//求最小公倍数
#include <stdio.h>
int f(int a,int b) //新定义一个函数 f
{
int p,q,t;
p=a>b?a:b; //p等于a,b中较大的那一个
q=a>b?b:a; //q等于a,b中较小的那一个
t=p;
while(1)
{
if(p%q==0)
break;
p+=t;
}
return p; //返回p的值
}
int main()
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d",f(x,y)); //调用函数f,值为返回的p
return 0;
}
运行结果:
3 4
12
--------------------------------
Process exited after 3.607 seconds with return value 0
请按任意键继续. . .