Advertisement
Schnuk

Untitled

Feb 25th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 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.     Fraction() { this->next = nullptr; }
  18. };
  19.  
  20. void OutputFareySequence(Fraction* array)
  21. {
  22.     Fraction* current = array;
  23.     while (current != NULL)
  24.     {
  25.         cout << current->numerator << " / " << current->denominator << endl;
  26.         current = current->next;
  27.     }
  28. }
  29.  
  30. void GetFareySequence(Fraction* array, int n)
  31. {
  32.     array[1] = Fraction(1, 1, nullptr);
  33.     array[0] = Fraction(0, 1, &array[1]);
  34.     int newFractionPosition = 2;
  35.     for (int i = 2; i <= n; i++)
  36.     {
  37.         Fraction* current = array;
  38.         while (current->next != NULL)
  39.         {
  40.             if (current->denominator + current->next->denominator == i)
  41.             {
  42.                 int newFractionNumerator = current->numerator + current->next->numerator;
  43.                 int newFracTionDenominator = current->denominator + current->next->denominator;
  44.                 array[newFractionPosition] = Fraction(newFractionNumerator, newFracTionDenominator, current->next);
  45.                 current->next = &array[newFractionPosition++];
  46.             }
  47.             current = current->next;
  48.         }  
  49.     }
  50. }
  51.  
  52. int main()
  53. {
  54.     for (int i = 0; i < 10000; i++)
  55.     {
  56.         struct Fraction array[10000];
  57.         const int arraySize = sizeof(array) / sizeof(array[0]);
  58.         int n = 10;
  59.         //cout << "Enter n (array size 100 elements)" << endl;
  60.         //cin >> n;
  61.         GetFareySequence(array, n);
  62.         OutputFareySequence(array);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement