HDU2503:a/b + c/d

來源:互聯網
上載者:User
Problem Description給你2個分數,求他們的和,並要求和為最簡形式。 

Input輸入首先包含一個正整數T(T<=1000),表示有T組測試資料,然後是T行資料,每行包含四個正整數a,b,c,d(0<1000),表示兩個分數a/b 和 c/d。
 

Output對於每組測試資料,輸出兩個整數e和f,表示a/b + c/d的最簡化結果是e/f,每組輸出佔一行。 

Sample Input

2 1 2 1 3 4 3 2 3 Sample Output
5 6 2 1 

 

 

#includeint yue(int m,int n)  //求分子分母的最大公約數{    int t,r;    if(m>n)    {        t = n;        n = m;        m = t;    }    while(m)    {        r = n%m;        n = m;        m = r;    }    return n;}int main(){    int t,a,b,c,d;    int n,m,k;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d%d",&a,&b,&c,&d);        m = b*d;    //分母通分        n = a*d+b*c;  //通分後的分子相加        if(m == n)  //分子與分母相同的狀況        {        printf("1 1\n");        continue;        }        k = yue(m,n);        printf("%d %d\n",n/k,m/k);  //分子與分母化簡    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.