c语言 求两个数的最大公约数
利用欧几里得算法: a = b*q + r (a > b, 0 < r < b)
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b);
int main()
{
int a = 0,b = 0;
printf("input a,b:");
scanf("%d %d", &a,&b);
printf("%d", gcd(a,b));
return 0;
}
int gcd(int a, int b)
{
int r = a;
if (b > a) //确保a>b提高程序健壮性
{
a = b;
b = r;
r = a;
}
if (a%b == 0)
return b;
r = a%b;
a = b;
b = r;
return gcd(a,b);
}