编程用试探法(要求从大到小试探)实现函数gcd(m,n),其功能为求解正整数m、n的最大公约数。

2025-03-22 03:07:27
推荐回答(2个)
回答1:

int gcd(int m,int n)
{
    int result=m>n?n:m;
    while(m%result || n%result)
    { 
        result--;
    }
    return result;
}

void main()
{
    scanf("%d,%d",&m,&n);
    printf("The result is %d !, gcd(m,n));
}

回答2:

#include
int gcd(int m, int n)
{
int gcd;
gcd=m>n?n:m;
while(m%gcd!=0 || n%gcd!=0)
{
gcd--;
}
return gcd;
}

void main()
{
int m, n;
scanf("%d,%d",&m,&n);
printf("The result is %d !, gcd(m,n));
}