编程实现功能:求2个数的最大公约数和最小公倍数。要求用一个函数求最大公约数,另外一个函数求最小公倍数

/*Name:a.cpp*/
#include<stdio.h>
#include"b.cpp"
#include"c.cpp"
void main()
{int num1,num2,r,n,m;
printf("please input two numbers:");
scanf("%d,%d",&num1,&num2);
if(num1<num2)
r=num1,num1=num2,num2=r;
n=num1;
m=num2;
maxa(n,m);
maxb(n,m);
}

/*Name:b.cpp*/
void maxa(int n,int m)
{ int i;
while(m!=0)
{ i=n%m;
n=m,m=i;
}
printf("GCD is:%d",n);

}

/*Name:c.cpp*/
void maxb(int n,int m)
{ int i,j,k,t;
k=n;
t=m;
while(m!=0)
{ i=n%m;
n=m,m=i;
}

j=k*t/n;
printf("LCM is:%d",j);
}

#include <stdio.h>

/* 最大公约数 */
int GreatestCommonDivisor ( int m, int n )
{
int r;
do
{
r = m % n;
m = n;
n = r;
} while ( r != 0 );
return m;
}

/* 最小公倍数 */
int LeastCommonMultiple ( int m, int n )
{
return m / GreatestCommonDivisor ( m , n ) * n;
}

int main (void)
{
int m, n;
printf ("请输入两个数字:" );
scanf ( "%d%d", &m, &n );
printf ("最大公约数是%d。\n", GreatestCommonDivisor(m, n) );
printf ("最小公倍数是%d。\n", LeastCommonMultiple(m, n) );
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-22
# include <stdio.h>
int gcd(int x,int y)//最大公约数
{
if (y==0)
return x;
else
return gcd(y,x%y);
}
int lcd (int x,int y,int z)//最小公倍数
{
return x*y/z;
}
int main()
{
int n,b,c;
scanf("%d",&n);
while (n--)
{
scanf("%d%d",&b,&c);
printf("%d\n",gcd(b,c));
}
return 0;
}
第2个回答  2012-04-20
问题都没有...
相似回答