zoukankan      html  css  js  c++  java
  • ACM模板——分数类

    #include <bits/stdc++.h>
    using namespace std;
    
    #define _for(i,a,b) for(int i = (a);i < (b);i ++)
    const int maxn = 50003;
    
    int gcd(int a,int b){if(b==0) return a;return gcd(b,a%b);}
    int lcm(int a,int b){return a/gcd(a,b)*b;}
    
    class Fraction
    {
        public:
            int a,b;
            int sign(int x) {return (x>0?1:-1);}
            Fraction():a(0),b(1){}
            Fraction(int x):a(x),b(1){}
            Fraction(int x,int y)
            {
                int m = gcd(abs(x),abs(y));
                a = x/m*sign(y);
                if(a==0)b=1;else b = abs(y/m);
            }
            int get_denominator() {return b;}
            int get_numerator() {return a;}
            Fraction operator+(const Fraction &f)
            {
                int m = gcd(b,f.b);
                return Fraction(f.b/m*a+b/m*f.a,b/m*f.b);
            }
            Fraction operator-(const Fraction &f)
            {
                int m = gcd(b,f.b);
                return Fraction(f.b/m*a-b/m*f.a,b/m*f.b);
            }
            Fraction operator*(const Fraction &f)
            {
                int m1 = gcd(abs(a),f.b);
                int m2 = gcd(b,abs(f.a));
                return Fraction((a/m1)*(f.a/m2),(b/m2)*(f.b/m1));
            }
            Fraction operator/(const Fraction &f)
                {return (*this)*Fraction(f.b,f.a);}
            friend ostream &operator << (ostream &out,const Fraction &f)
            {
                if(f.a==0) cout << 0;
                else if(f.b==1) cout << f.a;
                else cout << f.a << '/' << f.b;
                return out; 
            }
    };
    
    int main()
    {
        Fraction p(18,10);
        Fraction o(11,17);
        cout << p*o << endl; 
        return 0;
    }
    分数类
  • 相关阅读:
    4.26上午
    4.25下午
    4.19上午
    4.18上午
    7.26
    7.25
    7.21-7.22
    7.22
    7.21
    7.11-7.15
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10459878.html
Copyright © 2011-2022 走看看