求最大公约数伪代码

欧几里得算法求最大公约数伪代码

欧几里得算法

(欧几里得法原理和算法)

伪代码

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;
}

运行


posted @ 2022-10-03 14:46  瞻鹤  阅读(74)  评论(0编辑  收藏  举报