求最大公约数伪代码
欧几里得算法求最大公约数伪代码
欧几里得算法
伪代码
Begin
set a,b
read a
read b
set c to min{a,b};
set b to d
while(a%c != 0) do
{
d = c
c = a%c
a = d
}
printf c
end
代码实现
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
printf("输入两个数,求最大公因数:");
scanf("%d%d", &a, &b);
int c = a > b ? b : a;
int d = 0;
while (a % c != 0)
{
d = c;
c = a % c;
a = d;
}
printf("%d\n", c);
return 0;
}
运行