编写两种方法的C程序~~~

来源:百度知道 编辑:UC知道 时间:2024/05/31 13:50:41
求两个整数的最大公约数和最小公倍数,用一个函数求最大公约数,另一个函数根据求出的最大公倍数求最小公倍数。
1 用全局变量的方法,将两个整数的最大公约数,最小公倍数都设为全局变量。
2 不用全局变量,两个整数在主函数中输入,并传送给函数1,求出最大公约数返回给主函数,然后再与两个整数一起作为实参传递给函数2,以求出最小公倍数,最后返回到主函数输出最大公约数和最小公倍数。
拜托~~~~~

1、
#include<stdio.h>
int Max,Min;

int max(int s, int t)
{
if(!t)
Max=s;
else
Max=max(t, s%t);
return Max;
}

int min(int s, int t)
{
Min=s*t/Max;
return Min;
}

void main()
{
int a,b,c,d,t;
printf("please enter a&b\n");
scanf("%d,%d",&a,&b);
if(a<b)
{
t=a;
a=b;
b=t;
}
c=max(a,b);
d=min(a,b);
printf("the max is %d\nthe min is %d",c,d);
}
2、
#include<stdio.h>
int max(int s, int t)
{
if(!t)
return s;
else
return max(t, s%t);
}

int min(int s, int t)
{
return s*t/max(s, t);
}

void main()
{
int a,b,c,d,t;
printf("please enter a&b\n");
scanf("%d,%d",&a,&b);
if(a<b)
{
t=a