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 () {}
- };
- int main()
- {
- Fraction last = Fraction(1, 1, NULL);
- Fraction first = Fraction(0, 1, &last);
- Fraction array[100] = {};
- array[0] = first;
- array[1] = last;
- for (int i = 2; i < 100; i++)
- array[i] = Fraction();
- int n;
- cin >> n;
- int newFractionPosition = 2;
- for (int i = 2; i <= n; i++)
- {
- Fraction* current = &first;
- 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;
- }
- }
- int i = 0;
- Fraction current = array[0];
- while (current.next != NULL)
- {
- cout << current.numerator << " / " << current.denominator << endl;
- current = *current.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement