Sum of L1-009 N numbers (20 points)

Sum of L1-009 N numbers (20 points)

The requirement of this question is very simple, that is to find the sum of N numbers. The trouble is that these numbers are given in the form of rational numerator / denominator, and the sum you output must also be in the form of rational number.

Input format:

Enter the first line to give a positive integer N (< 100). The next line is in the format a1/b1 a2/b2 Give N rational numbers. Ensure that all molecules and denominators are in the long form. In addition, the sign of a negative number must appear in front of the molecule.

Output format:

The simplest form for outputting the sum of the above numbers - that is, the result is written as the fractional part of the integer part, where the fractional part is written as the numerator / denominator, the numerator is required to be smaller than the denominator, and they have no common factors. If the integer part of the result is 0, only the fraction part is output.

Enter example 1:

5
2/5 4/15 1/30 -2/60 8/3

Output example 1:

3 1/3

Enter example 2:

2
4/3 2/3

Output example 2:

2

Enter example 3:

3
1/3 -1/6 1/8

Output example 3:

7/24

Code

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long b)
{
    return b==0?a:gcd(b,a%b);
}
long long lcm(long long a, long long b)
{
    return a*b/gcd(a,b);
}
vector<string> split(const string &str, const char c)
{
    string buff = "";
    vector<string> vec;
    for(int i=0;i<str.size();i++)
    {
        if(str[i] == c && buff != "")
        {
            vec.push_back(buff);
            buff = "";
        }
        else
            buff += str[i];
    }
    if(buff != "")
        vec.push_back(buff);
    return vec;
}
struct Rational
{
    long long up;
    long long down;
    Rational(){}
    Rational(string str)
    {
        vector<string> tmp = split(str,'/');
        up = atoi(tmp[0].c_str());
        if(tmp.size() > 1)
            down = atoi(tmp[1].c_str());
        else
            down = 1;
    }
    string to_string()
    {
        string str;
        if(up == 0 || down == 0)
            return str="0";
        long long z = abs(up)/down;
        if(up < 0)
            z = -z;
        if(z != 0)
            str = std::to_string(z);
        if(up%down != 0)
        {
            if(z != 0)
                str += " ";
            long long g = gcd(up%down, down);
            long long uu = (up%down/g);
            str+= std::to_string(uu);
            str+= "/"+std::to_string(down/g);
        }
        return str;
    }
};
Rational add(const Rational &a, const Rational &b)
{
    if(b.up == 0 || b.down == 0)
        return a;
    if(a.up == 0 || a.down == 0)
        return b;
    long long l = lcm(abs(a.down), abs(b.down));
    Rational res;
    res.down = l;
    res.up = a.up * (l/a.down) + b.up * (l/b.down);
    long long g = gcd(abs(res.up), abs(res.down));
    res.up /= g;
    res.down /= g;
    return res;
}
int main()
{
    int n;
    cin >> n;
    string str;
    queue<Rational> rv;
    for(int i=0;i<n;i++)
    {
        cin >> str;
        Rational temp(str);
        rv.push(temp);
    }
    Rational a = rv.front(); rv.pop();
    while(!rv.empty())
    {
        Rational b = rv.front(); rv.pop();
        a = add(a,b);
    }
    cout << a.to_string();
    return 0;
}

Posted by bubblybabs on Sat, 07 Dec 2019 20:52:38 -0800