q123456fuhuhfuh 2021-06-06 20:37 采纳率: 33.3%
浏览 116
已采纳

C++递归求最大公约数

急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11

  • 写回答

5条回答 默认 最新

  • 小P聊技术 2021-06-06 20:42
    关注
    // This program demonstrates a recursive function to
    // calculate the greatest common divisor (gcd) of two numbers.
    #include <iostream>
    using namespace std;
    
    // Function prototype
    int gcd(int, int);
    int main()
    {
        int num1, num2;
        cout << "Enter two integers: ";
        cin >> num1 >> num2;
        cout << "The greatest common divisor of " << num1;
        cout << " and " << num2 << " is ";
        cout << gcd(num1, num2) << endl;
        return 0;
    }
    int gcd(int x, int y)
    {
        if (x % y == 0) //base case
            return y;
        else
            return gcd{y, x % y);
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?