-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob33.cpp
More file actions
63 lines (54 loc) · 1.15 KB
/
prob33.cpp
File metadata and controls
63 lines (54 loc) · 1.15 KB
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
#include <iostream>
using namespace std;
inline int gcd(int a, int b)
{
if (a == 0) return b;
while (b != 0) {
if (a > b) a -= b;
else b -= a;
}
return a;
}
inline int reduce(int &num, int &den)
{
int hcf = gcd(num, den);
num /= hcf;
den /= hcf;
}
bool CanCancelFraction(int numerator, int denominator)
{
int num_a = numerator / 10;
int num_b = numerator % 10;
int den_a = denominator / 10;
int den_b = denominator % 10;
int n, d;
if (num_a == den_a) {
n = num_b;
d = den_b;
} else if (num_a == den_b) {
n = num_b;
d = den_a;
} else if (num_b == den_a) {
n = num_a;
d = den_b;
} else if (num_b == den_b) {
n = num_a;
d = den_a;
} else {
return false;
}
reduce(numerator, denominator);
reduce(n, d);
return ((numerator == n) && (denominator == d));
}
int main()
{
for (int i = 10; i <= 99; ++i) {
for (int j = i + 1; j <= 99; ++j) {
if (CanCancelFraction(i, j)) {
cout << i << "/" << j << endl;
}
}
}
return 0;
}