Advertisement
Schnuk

Untitled

Feb 25th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 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, Fraction** memoryArray)
  32. {
  33.     memoryArray[0] = new Fraction(0, 1, &array[1]);
  34.     memoryArray[1] = new Fraction(1, 1, NULL);
  35.     array[1] = *memoryArray[1];
  36.     array[0] = *memoryArray[0];
  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.                 memoryArray[newFractionPosition] = newFraction;
  49.                 current->next = newFraction;
  50.                 array[newFractionPosition++] = *newFraction;
  51.             }
  52.             current = current->next;
  53.         }
  54.     }
  55. }
  56.  
  57. int main()
  58. {
  59.     for (int i = 0; i < 10000; i++)
  60.     {
  61.         struct Fraction array[10000];
  62.         const int arraySize = sizeof(array) / sizeof(array[0]);
  63.         Fraction* memoryArray[arraySize];
  64.         for (int i = 0; i < 100; i++)
  65.             memoryArray[i] = nullptr;
  66.         int n = 10;
  67.         //cout << "Enter n (array size 100 elements)" << endl;
  68.         //cin >> n;
  69.         GetFareySequence(array, n, memoryArray);
  70.         OutputFareySequence(array);
  71.         for (int i = 0; i < 100; i++)
  72.             delete memoryArray[i];
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement