zoukankan      html  css  js  c++  java
  • 分数相加减的代码(c++)

    #include <iostream>
    using namespace std;
    int gy(int a,int k1)
    {int min;
    if(a>k1)min=k1;
    else min=a;
        while(min)
        {   if(a%min==0&&k1%min==0)
               {if(k1/min==1) cout<<a/min<<endl;
                   else cout<<a/min<<'/'<<k1/min<<endl;
                break;  
                }
             min--;
        }
    return 0;
    }
    int main()
    {int a,b,c,d,k1,g;
        char x1,j1,j2;
        while(cin>>a>>j1>>b>>x1>>c>>j2>>d)
        {k1=b*d;
        a=a*d;c=b*c;
        if(x1=='-')
            {a=a-c;
            if(a==0)
                cout<<'0'<<endl;
            if(a<0){a=-a;cout<<"-";gy(a,k1);}
            else {gy(a,k1);}
             }
             if(x1=='+')
             {a=c+a;
             if(a==0)
                cout<<'0'<<endl;
             else gy(a,k1);
             }
    }
        cout<<endl;
    return 0;
    }
  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/lxh-boke/p/4537206.html
Copyright © 2011-2022 走看看