-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrac1.cpp
executable file
·56 lines (52 loc) · 1.31 KB
/
frac1.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
/*
ID: paulius10
PROG: frac1
LANG: C++
*/
#include <fstream>
using namespace std;
struct fraction {
int numerator, denominator;
fraction *previous;
fraction *next;
};
int main() {
ifstream fin("frac1.in");
int N;
fin >> N;
fin.close();
fraction start;
start.numerator = 0;
start.denominator = 1;
fraction end;
end.numerator = 1;
end.denominator = 1;
start.previous = NULL;
start.next = &end;
end.previous = &start;
end.next = NULL;
bool changesDone = true;
while (changesDone) {
changesDone = false;
for (fraction *current = &start; current != &end; current = current->next)
{
int newDenominator = current->denominator + current->next->denominator;
if (newDenominator <= N) {
changesDone = true;
fraction* newFraction = new fraction;
newFraction->numerator = current->numerator + current->next->numerator;
newFraction->denominator = newDenominator;
newFraction->previous = current;
newFraction->next = current->next;
current->next->previous = newFraction;
current->next = newFraction;
}
}
}
ofstream fout("frac1.out");
for (fraction *f = &start; f != NULL; f = f->next) {
fout << f->numerator << "/" << f->denominator << endl;
}
fout.close();
return 0;
}