欧几里德算法
1 /* 2 欧几里德算法:辗转求余 3 原理: gcd(a,b)=gcd(b,a mod b) 4 当b为0时,两数的最大公约数即为a 5 getchar()会接受前一个scanf的回车符 6 */ 7 #include<stdio.h> 8 unsigned int Gcd(unsigned int M,unsigned int N) 9 { 10 unsigned int Rem; 11 while(N > 0) 12 { 13 Rem = M % N; 14 M = N; 15 N = Rem; 16 } 17 return M; 18 } 19 int main(void) 20 { 21 int a,b; 22 scanf("%d %d",&a,&b); 23 printf("the greatest common factor of %d and %d is ",a,b); 24 printf("%d\n",Gcd(a,b)); 25 return 0; 26 }
来自https://baike.baidu.com/item/%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%B7%E7%AE%97%E6%B3%95/9002848?fr=aladdin