-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction.cpp
78 lines (64 loc) · 2.35 KB
/
Fraction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
struct Fraction {
ll numerator, denominator;
Fraction (ll n = 0, ll d = 1) {
ll g = gcd (n, d);
n /= g;
d /= g;
if (d < 0)n *= -1;
d = abs (d);
if (d == 0)n = abs (n);
numerator = n;
denominator = d;
}
Fraction operator+ (const Fraction &o) const {
return Fraction (numerator * o.denominator + denominator * o.numerator, denominator * o.denominator);
}
Fraction operator+ (const ll &o) const {
return *this + Fraction (o, 1);
}
Fraction operator- (const Fraction &o) const {
return Fraction (numerator * o.denominator - denominator * o.numerator, denominator * o.denominator);
}
Fraction operator* (const Fraction &o) const {
return Fraction (numerator * o.numerator, denominator * o.denominator);
}
Fraction operator/ (const Fraction &o) const {
return Fraction (numerator * o.denominator, denominator * o.numerator);
}
bool operator< (const Fraction &o) const {
return numerator * o.denominator < o.numerator * denominator;
}
bool operator== (const Fraction &o) const {
return numerator * o.denominator == o.numerator * denominator;
}
bool operator<= (const Fraction &o) const {
return *this < o or *this == o;
}
bool operator> (const Fraction &o) const {
return !(*this <= o);
}
bool operator>= (const Fraction &o) const {
return !(*this < o);
}
friend ostream &operator<< (ostream &out, const Fraction &f) {
out<<f.numerator<<"/"<<f.denominator;
return out;
}
friend Fraction smallest_fraction_in_interval (Fraction a, Fraction b);
};
//满足 a/b < p/q < c/d 的分数 p/q,分母最小的是多少
Fraction smallest_fraction_in_interval (Fraction a, Fraction b) {
if (b < a)swap (a, b);
if (a.numerator >= a.denominator) {
ll d = a.numerator / a.denominator;
a.numerator -= a.denominator * d;
b.numerator -= b.denominator * d;
return smallest_fraction_in_interval (a, b) + d;
}
if (b.numerator > b.denominator)return Fraction (1, 1);
swap (a.numerator, a.denominator);
swap (b.numerator, b.denominator);
auto ans = smallest_fraction_in_interval (b, a);
swap (ans.numerator, ans.denominator);
return ans;
}