遞迴_輾轉相除法

 avatar
user_3763047219
c_cpp
2 years ago
622 B
2
Indexable
int GCD(int t, int v);

int GCD(int t, int v) {

    if (t > v) {
        t = t - (t / v) * v;
    }
    else {
        v = v - (v / t) * t;
    }

    if (t != 0 && v != 0) {
        GCD(t, v);
    }
    else {
        if (t == 0) {
            return v;
        }
        else if (v == 0) {
            return t;
        }
    }
}


#include <stdio.h>
#include <stdlib.h>

int main() {
    int a, b;

    scanf("%d", &a);
    scanf("%d", &b);
    int temp;

    if (b > a) {
        temp = a;
        a = b;
        b = temp;
    }
    printf("%d\n", GCD(a, b));
    return 0;
}
Editor is loading...