求大哥大姐mem帮忙。。。~~

来源:百度知道 编辑:UC知道 时间:2024/05/12 05:25:27
编写一个求两个数的最大公约数的函数,并在主函数中调用这个函数验证结果是否正确.

这是期末考试试题。我弄了半天想不出。。

用户任意两个数,计算这两个数的最大公约数和最小公倍数。
程序如下:
#include<stdio.h>
void main()
{
int a,b,x=1,max;
printf("请输入a=");
scanf("%d",&a);
printf("请输入b=");
scanf("%d",&b);
for(x;x<=a&&x<=b;x++)
{
if(a%x==0&&b%x==0)
max=x;
}
printf("最大公约数为:%d\n",max);
printf("最小公倍数为:%d\n",a*b/max);
}

#include <stdio.h>

int func(int a,int b)
{
int temp;
if(a<b)
return func(b,a);
while(1)
{
if(a%b==0)
return b;
else
{
temp=b;
b=a%b;
a=temp;
}
}
}

int main()
{
printf("%d\n",func(36,48));
}

#include <iostream.h>
int gcd(int x,int y)
{
if (x==0)
{
return y;
}
else if (y<x)
{
return gcd(y,x);
}
else <