用辗转取余法求最大公约数的关键在于借助中间变量temp,完成大值和小值的转换和赋值。其中,大值取余小值得到结果赋给小值,除数赋给大值,循环退出时小值一定为零,最大公约数为大值或temp。
#include<stdio.h>
int main()
{
int i, j, temp;
int m, n;
scanf("%d%d", &i, &j);
m = i;
n = j;
if(i < j)
{
temp = i;
i = j;
j = temp;
}// i is the bigger value and j is the smaller value here.
while(j != 0)
{
temp = j;
j = i % j;
i = temp;
}
printf("%d", i);
printf("\n%d", m * n / temp);
return 0;
}