zoukankan      html  css  js  c++  java
  • LC 592. Fraction Addition and Subtraction

    Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2, you need to change it to the format of fraction that has denominator 1. So in this case, 2 should be converted to 2/1.

    Example 1:

    Input:"-1/2+1/2"
    Output: "0/1"
    

    Example 2:

    Input:"-1/2+1/2+1/3"
    Output: "1/3"
    

    Example 3:

    Input:"1/3-1/2"
    Output: "-1/6"
    

    Example 4:

    Input:"5/3+1/3"
    Output: "2/1"
    

    Note:

    1. The input string only contains '0' to '9''/''+' and '-'. So does the output.
    2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
    3. The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
    4. The number of given fractions will be in the range [1,10].
    5. The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
    Runtime: 8 ms, faster than 0.00% of C++ online submissions for Fraction Addition and Subtraction.
    Memory Usage: 5.1 MB, less than 0.00% of C++ online submissions for Fraction Addition and Subtraction.
    class Solution {
    public:
        long long gcd(long long a, long long b) {
            if(a < b) return gcd(b,a);
            if(b == 0) return a;
            return gcd(b, a%b);
        }
        string fractionAddition(string expression) {
            vector<int> numerator;
            vector<int> denominator;
            int j = 0;
            bool positive = false;
            if(expression[0] >= '0' && expression[0] <= '9') positive = true;
            else {
                positive = false;
                j = 1;
            }
            string tmp;
            for(size_t i=j+1; i<expression.size(); i++) {
                if(expression[i] == '+' || expression[i] == '-') {
                    tmp = expression.substr(j, i-j);
                    size_t k;
                    for(k = 0; k < tmp.size(); k++) {
                        if(tmp[k] == '/') break;
                    }
                    numerator.push_back(stoi(tmp.substr(0,k)));
                    if(!positive) numerator[numerator.size()-1] *= -1;
                    positive = expression[i] == '+' ? true : false;
                    denominator.push_back(stoi(tmp.substr(k+1)));
                    j = i+1;
                }
            }
            tmp = expression.substr(j,expression.size()-j);
            size_t k;
            for(k=0; k<tmp.size(); k++)
                if(tmp[k] == '/') break;
            numerator.push_back(stoi(tmp.substr(0,k)));
            if(!positive) numerator[numerator.size()-1] *= -1;
            denominator.push_back(stoi(tmp.substr(k+1)));
    // test
    
    
    
    
            long long ret_numer = numerator[0];
            long long ret_denomi = denominator[0];
            long long r = 0;
            for(size_t i= 1; i<denominator.size(); i++) {
                long long tmp_deno = ret_denomi;
                ret_denomi *= denominator[i];
                ret_numer = ret_numer * denominator[i] + tmp_deno * numerator[i];
            }
            if(ret_numer > 0) {
                r = gcd(ret_numer, ret_denomi);
                ret_numer /= r;
                ret_denomi /= r;
            } else if (ret_numer < 0) {
                r = gcd(-ret_numer, ret_denomi);
                ret_numer /= r;
                ret_denomi /= r;
            } else if(ret_numer == 0) {
                ret_denomi = 1;
            }
            string str_numer = ret_numer < 0 ? ("-" + to_string(-ret_numer)) : to_string(ret_numer);
            string str_deno  = to_string(ret_denomi);
            return str_numer + "/" + str_deno;
      }
    };

     更好的一个思路

    class Solution {
    public:
        string fractionAddition(string expression) {
            istringstream iss(expression);
            int num = 0, den = 0, NUM = 0, DEN = 1;
            char c;
            while (iss >> num >> c >> den)
            {
                NUM = NUM * den + num * DEN;
                DEN *= den;
                int g = abs(gcd(NUM, DEN));
                NUM /= g;
                DEN /= g;
            }
            
            return to_string(NUM) + "/" + to_string(DEN);
        }
        
        int gcd(int x, int y)
        {
            return y == 0 ? x : gcd(y, x % y);
        }
    };
  • 相关阅读:
    luogu1117 优秀的拆分 (后缀数组)
    hdu5238 calculator (线段树+crt)
    [模板]中国剩余定理/扩展中国剩余定理
    [模板]欧几里得算法/扩展欧几里得
    cf1088E Ehab and a component choosing problem (树形dp)
    cf1088D Ehab and another another xor problem (构造)
    cf1088C Ehab and a 2-operation task (构造)
    luogu3292 幸运数字 (点分治+线性基)
    2017-03-10<Git版本回退>
    2017-03-09<AS目录结构>
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10354343.html
Copyright © 2011-2022 走看看