七.請以任何一種高階程式語言,撰寫計算最大公因數(gcd)的遞迴演算法。 並以計算gcd(31031,10013)為例逐步展示此演算法過程。(10分)

詳解 (共 1 筆)

詳解 提供者:陳柏伸

https://www.csie.ntu.edu.tw/~b98902112/cpp_and_algo/cpp02/euclidean_algorithm.html


#include<iostream>
using namespace std;

int gcd( int a, int b )
{
    if( b==0 )
        return a;
    return f( b, a%b );
}

int main()
{
    int a=31031, b=10013;


        cout << gcd(a,b) << endl;

    return 0;
}