参考:
代码例子(用C语言):
/*
辗转相除法求最大公约数: 原理:https://zhidao.baidu.com/question/1955193414795316988.html
知乎:https://zhuanlan.zhihu.com/p/465808497
如果b等于0,计算阶数,a就是最大公约数;
否则,计算a除以b的余数,让a等于b,而b等于那个余数;
回到第一步
*/
#include <stdio.h>
int main21()
{
int a,b; //定义输入的两个数
int t; //定义a÷b的余数
scanf("%d %d",&a,&b);
while(b!=0){
t=a%b;
a=b;
b=t;
printf("a=%d,b=%d,t=%d\n",a,b,t);
}
printf("gcd=%d\n",a);
return 0;
}