在学习C语言的工程中,发现了一种新的求最大公约数和公倍数的方法:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int g(int ,int);//g()函数代表最大公约数
int s(int,int);//s()代表最小公倍数
int i,j;
int a,b;
printf("please input the two numbers:\n");
scanf("%d,%d",&a,&b);
if(a>=b)
{
i=g(a,b);
j=s(a,b);
}
else
{
i=g(b,a);
j=s(b,a);
}
printf("The max is %d,The small is %d",i,j);
return 0;
}
int g(int x,int y)
{
while((x%y)!=0)
{
y=int(x%y);
}
return y;
}
int s(int x,int y)
{
int k;
k=x*y;
while((x%y)!=0)
{
y=int(x%y);
}
k=k/y;
return k;
}