跳到正文

剪贴板

剪贴板 ukfxrosk

原文
剪贴板信息

剪贴板信息

收录
更新

分数板子

C++
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
struct frac
{
	public:
		ll x,y;//x/y
	private:
		void yf(){if(y<0)x=-x,y=-y;ll a=gcd(abs(x),y);x/=a,y/=a;}
	public:
		frac(ll x=0,ll y=1):x(x),y(y){yf();}
		double todb(){return (double)x/(double)y;}
		frac operator=(frac b){x=b.x,y=b.y;return *this;}
};
frac operator+(frac a,frac b){ll d=gcd(a.y,b.y);b.y/=d,a.y/=d;return frac(a.x*b.y+a.y*b.x,d*a.y*b.y);}
frac operator-(frac a){return frac(-a.x,a.y);}
frac operator-(frac a,frac b){return a+(-b);}
frac operator*(frac a,frac b){return frac(a.x*b.x,a.y*b.y);}
frac operator/(frac a,frac b){return frac(a.x*b.y,a.y*b.x);}
frac operator+=(frac& a,frac b){return a=a+b;}
frac operator-=(frac& a,frac b){return a=a-b;}
frac operator*=(frac& a,frac b){return a=a*b;}
frac operator/=(frac& a,frac b){return a=a/b;}
bool operator>(frac a,frac b){return a.x*b.y>b.x*a.y;}
bool operator<(frac a,frac b){return b>a;}
bool operator>=(frac a,frac b){return !(b>a);}
bool operator<=(frac a,frac b){return !(a>b);}
bool operator==(frac a,frac b){return !(a<b)&&!(b<a);}
bool operator!=(frac a,frac b){return (a<b)||(b<a);}