Advertisement
Schnuk

Untitled

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