Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Fraction
- {
- int numerator;
- int denominator;
- Fraction* next;
- Fraction(int numerator, int denominator, Fraction* next)
- {
- this->numerator = numerator;
- this->denominator = denominator;
- this->next = next;
- }
- Fraction() {}
- };
- void OutputFareySequence(Fraction* array)
- {
- Fraction* current = &array[0];
- while (current != NULL)
- {
- cout << current->numerator << " / " << current->denominator << endl;
- current = current->next;
- }
- }
- void GetFareySequence(Fraction* array, int n)
- {
- Fraction* first = new Fraction(1, 1, NULL);
- Fraction* last = new Fraction(0, 1, &array[1]);
- array[1] = *first;
- array[0] = *last;
- int newFractionPosition = 2;
- for (int i = 2; i <= n; i++)
- {
- Fraction* current = &array[0];
- while (current->next != NULL)
- {
- if (current->denominator + current->next->denominator == i)
- {
- int newFractionNumerator = current->numerator + current->next->numerator;
- int newFracTionDenominator = current->denominator + current->next->denominator;
- Fraction* newFraction = new Fraction(newFractionNumerator, newFracTionDenominator, current->next);
- current->next = newFraction;
- array[newFractionPosition++] = *newFraction;
- }
- current = current->next;
- }
- }
- delete first; delete last;
- }
- int main()
- {
- for (int i = 0; i < 10000; i++)
- {
- struct Fraction array[100];
- int n = 10;
- //cout << "Enter n (array size 100 elements)" << endl;
- //cin >> n;
- GetFareySequence(array, n);
- OutputFareySequence(array);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement