辗转相除法:
如果b=0,计算结束,a就是最大公约数;
否则,计算a除以b的余数,让a=b,而b等于那个余数;
回到第一步
a b t
12 18 12
18 12 6
12 6 0
6 0
1、辗转相处法求最大公约数
#include <stdio.h>
int main()
{
int a,b;
int t;
scanf("%d %d",&a,&b);
while(b!=0){
t=a%b;
a=b;
b=t;
}
printf("gcd=%d\n",a);
return 0;
}
2、化简分式.
辗转相除法求最大公约数化简分式
#include <stdio.h>
int main()
{
int dividend,divisor;
scanf("%d/%d",÷nd,&divisor);
int a=dividend;
int b=divisor;
int t;
while(b>0){ //辗转相除法求最大公约数
t=a%b;
a=b;
b=t;
}
printf("%d/%d\n",dividend/a,divisor/a);
return 0;
}