zoukankan      html  css  js  c++  java
  • F

    Mr. Potato Head has been promoted and now is a math professor at the UNAL.

    For his first course he is willing to teach hard subjects, so at the moment he is teaching how to add and subtract fractions.

    To complete his course the students have to do a long series of exercises, each exercise corresponds to a valid formula containing only additions and subtractions of fractions.

    Formally a valid formula is one of the following:

    • A fraction
    • F1+F2F1+F2
    • F1F2F1−F2
    • (F1)(F1)

    where F1F1 and F2F2 are also valid formulas.

    Mr. Potato Head knows that the exam would be impossible if fractions are too large or if they are negative, so he decides that for every fraction a/ba/b, 0a1000≤a≤100 and 0<b200<b≤20.

    Can you pass the course of Mr. Potato Head?

    Input

    The input consists of several lines, each line contains a valid formula without spaces.

    It is guaranteed that all lines contains valid formulas and the total number of characters in all formulas does not exceed 21052∗105

    Output

    For each formula output a line with an irreducible fraction a/ba/b, b>0b>0 − The solution of the corresponding formula

    Example

    Input
    1/2+1/3
    1/5-2/10
    1/2+(1/2-2/1)
    
    Output
    5/6
    0/1
    -1/1
    

    Note

    A fraction is irreducible if its numerator and denominator do not have common divisors greater than 1

    题解:只有加减法和括号的运算,可直接从前往后挨个计算,只需判断当前的符号是+或-即可.

    #include <bits/stdc++.h>
    #define met(a, b) memset(a, b, sizeof(a))
    #define ll long long
    #define ull unsigned long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    using namespace std;
    ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
    ll lcm(ll a,ll b){return a*b/gcd(a,b);}
    typedef pair<ll,ll>P;
    const int maxn=200010;
    const double eps=1e-10;
    const double pi=acos(-1);
    char s[maxn];
    int len;
    P ans;
    int f,ff;
    P cal(P ans,P now)
    {
      ll son=ans.first*now.second+f*ff*now.first*ans.second;
      ll mon=ans.second*now.second;
      ll g=gcd(abs(son),abs(mon));
      son/=g;
      mon/=g;
      return P(son,mon);
    }
    P ok(int pos)
    {
      ll son=0,mon=0,l=-1;
      for(int i=pos-1;i>=0;i--){
        if(s[i]<'0'||s[i]>'9'){
          l=i;
          break;
        }
      } 
      for(int i=l+1;i<len;i++){
        if(s[i]>='0'&&s[i]<='9')son=son*10+s[i]-'0';
        else break;
      }
      for(int i=pos+1;i<len;i++){
        if(s[i]>='0'&&s[i]<='9')mon=mon*10+s[i]-'0';
        else break;
      }
      return P(son,mon);
    }
    int main()
    {
      while(cin>>s){
        stack<int>sta;
        len=strlen(s);
        ans=P(0,1);
        f=ff=1;
        for(int i=0;i<len;i++){
          if(s[i]=='+'){
            f=1;
          }
          else if(s[i]=='-'){
            f=-1;
          }
          else if(s[i]=='('){
            f=1;
            if(s[i-1]=='-'){
              ff*=-1;
              sta.push(-1);
            }
            else{
              sta.push(1);
            }
          }
          else if(s[i]==')'){
            int w=sta.top();
            ff*=w;
            sta.pop();
          }
          else if(s[i]=='/'){
            P now=ok(i);
            ans=cal(ans,now);
          }
        }
        cout<<ans.first<<"/"<<ans.second<<endl;
      }
      return 0;
    }
  • 相关阅读:
    JQuery autocomplete选中某项后触发事件
    Javascript类的写法
    windows2003里的数据库没被访问到
    SQL SERVER 2008 评估期已过,哪位大哥有解决方法?
    js对json增删改查操作
    javascript 谁才是initialize方法
    JavaScript的写类方式(1)
    我所收集的googlemap 应用实例
    经典的正则表达式工具Regulator使用教程(图文)
    Map的使用
  • 原文地址:https://www.cnblogs.com/cherish-lin/p/11516917.html
Copyright © 2011-2022 走看看