阿摩線上測驗 登入

申論題資訊

試卷:100年 - 100 經濟部所屬事業機構_新進職員甄試_資訊:1.資訊管理、2.程式設計#38137
科目:國營事業◆1.資訊管理 2.程式設計
年份:100年
排序:0

申論題內容

七.請以任何一種高階程式語言,撰寫計算最大公因數(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;
}