newcoder PAT 1001 Rational Sum (20)

Posted jawen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了newcoder PAT 1001 Rational Sum (20)相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int gcd(int a,int b)

    return b == 0 ? a: gcd(b,a%b);

int s[105];
int x[105];
int main()

    int n;
    cin >> n;
    for(int i = 0;i < n; ++i)
    
        int a,b;
        scanf("%d/%d",&a,&b);
        int g = gcd(a,b);
        s[i] = a/g;
        x[i] = b/g;
    
    int smallg = 1;
    for(int i = 0;i < n; ++i)
    
        int g = gcd(smallg,x[i]);
        smallg = smallg * x[i] / g;
    
    int sum = 0;
    for(int i = 0;i < n; ++i)
    
        sum += (smallg / x[i])*s[i];
    
    int g = gcd(sum,smallg);
    sum = sum / abs(g);
    smallg = smallg / abs(g);
    if(sum > 0)
    
        if(sum % smallg == 0)
        
            cout << sum / smallg << endl;
        
        else if(sum / smallg == 0)
            cout << sum % smallg << "/" << smallg << endl;
        else
            cout << sum / smallg << " " << sum % smallg << "/" << smallg << endl;
    
    else
    
        if(sum % smallg == 0)
        
            cout << sum / smallg << endl;
        
        else if(sum / smallg == 0)
            cout << "-" << (-sum) % smallg << "/" << smallg << endl;
        else
            cout << "-"<< (-sum) / smallg << " " << (-sum) % smallg << "/" << smallg << endl;
    




 

以上是关于newcoder PAT 1001 Rational Sum (20)的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1001. A+B Format

PAT-1001

PAT甲级1001

PAT Advanced 1001

pat 1001

PAT--乙级-1001