两个整型值M,N(M和N均大于零)的最大公约数可以按照下列方法计算:
当M%N=0时,gcd(M,N)=N;
当M%N=R,R>0时gcd(M,N)=gcd(N,R);
代码如下:
#include<stdio.h>
int gcd(int m, int n)
{
int r = 0;
r = m%n;
if (r<0)
return 0;
else if (r == 0)
return n;
else if (r>0)
return gcd(n, r);
}
int main()
{
int N = 0;
int M = 0;
int gc = 0;
scanf("%d%d", &N, &M);
gc = gcd(M, N);
printf("%d\n", gc);
system("pause");
return 0;
}
转载于:https://blog.51cto.com/haipi/1712902