Advertisement
Schnuk

Untitled

Feb 25th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Fraction
  6. {
  7.     int numerator;
  8.     int denominator;
  9.     Fraction* next;
  10.  
  11.     Fraction(int numerator, int denominator, Fraction* next)
  12.     {
  13.         this->numerator = numerator;
  14.         this->denominator = denominator;
  15.         this->next = next;
  16.     }
  17.  
  18.     Fraction() {}
  19. };
  20.  
  21. void OutputFareySequence(Fraction* array)
  22. {
  23.     Fraction* current = &array[0];
  24.     while (current != NULL)
  25.     {
  26.         cout << current->numerator << " / " << current->denominator << endl;
  27.         current = current->next;
  28.     }
  29. }
  30.  
  31. void GetFareySequence(Fraction* array, int n)
  32. {
  33.     Fraction* first = new Fraction(1, 1, NULL);
  34.     Fraction* last = new Fraction(0, 1, &array[1]);
  35.     array[1] = *first;
  36.     array[0] = *last;
  37.     int newFractionPosition = 2;
  38.     for (int i = 2; i <= n; i++)
  39.     {
  40.         Fraction* current = &array[0];
  41.         while (current->next != NULL)
  42.         {
  43.             if (current->denominator + current->next->denominator == i)
  44.             {
  45.                 int newFractionNumerator = current->numerator + current->next->numerator;
  46.                 int newFracTionDenominator = current->denominator + current->next->denominator;
  47.                 Fraction* newFraction = new Fraction(newFractionNumerator, newFracTionDenominator, current->next);
  48.                 current->next = newFraction;
  49.                 array[newFractionPosition++] = *newFraction;
  50.             }
  51.             current = current->next;
  52.         }
  53.     }
  54.     delete first; delete last;
  55. }
  56.  
  57. int main()
  58. {
  59.     for (int i = 0; i < 10000; i++)
  60.     {
  61.         struct Fraction array[100];
  62.         int n = 10;
  63.         //cout << "Enter n (array size 100 elements)" << endl;
  64.         //cin >> n;
  65.         GetFareySequence(array, n);
  66.         OutputFareySequence(array);
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement