HDU2503 a/b + c/d

问题链接HDU2503 a/b + c/d

问题简述:参见上述链接。

问题分析

  这是一个有关分数计算的问题。

  需要计算最大公约数。

程序说明:(略)。

AC的C++语言程序如下:

/* HDU2503 a/b + c/d */

#include <iostream>

using namespace std;

// 非递归计算最大公约数
int gcd(int m, int n)
{
    for(;;) {
        if(n == 0)
            return m;
        int temp = m % n;
        m = n;
        n = temp;
    }
}

int main()
{
    int t, a, b, c, d;
    int numerator, decimal, gcdval;

    cin >> t;
    while(t--) {
        cin >> a >> b >> c >> d;

        numerator = a * d + b * c;
        decimal = b * d;

        gcdval = gcd(numerator, decimal);

        cout << numerator / gcdval << " " << decimal / gcdval << endl;
    }

    return 0;
}




原文地址:https://www.cnblogs.com/tigerisland/p/7563945.html