分数模板

直接上代码

点击显/隐代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<bits/stdc++.h>
using namespace std;
inline int gcd(int a,int b) {return b==0||a==0?a+b:gcd(b,a%b);}
struct frac
{
int x,y;
inline frac yue() {return (frac){x/gcd(x,y),y/gcd(x,y)};}
inline frac dao() {return (frac){y,x};}
inline void read() {scanf("%d%d",&x,&y);}
inline void write() {printf("%d/%d\n",x,y);}
inline bool operator<(frac a) const {return x*a.y<y*a.x;}
inline bool operator==(frac a) const {return x*a.y==y*a.x;}
inline bool operator<=(frac a) const {return *this<a||*this==a;}
inline bool operator>(frac a) const {return !(*this<=a);}
inline bool operator>=(frac a) const {return !(*this<a);}
inline bool operator!=(frac a) const {return !(*this==a);}
inline frac operator+(frac a) const {return (frac){x*a.y+y*a.x,y*a.y}.yue();}
inline frac operator-(frac a) const {return (frac){x*a.y-y*a.x,y*a.y}.yue();}
inline frac operator*(frac a) const {return (frac){x*a.x,y*a.y}.yue();}
inline frac operator/(frac a) const {return *this*a.dao();}
};
int main()
{}