Advertisement
Schnuk

Untitled

Feb 23rd, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 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.  
  20. int main()
  21. {
  22. Fraction last = Fraction(1, 1, NULL);
  23. Fraction first = Fraction(0, 1, &last);
  24. Fraction* array[100] = {};
  25. array[0] = &first;
  26. array[1] = &last;
  27. for (int i = 2; i < 100; i++)
  28. array[i] = 0;
  29. int n;
  30. cin >> n;
  31. int newFractionPosition = 2;
  32. for (int i = 2; i <= n; i++)
  33. {
  34. Fraction* current = &first;
  35. while (current->next != NULL)
  36. {
  37. if (current->denominator + current->next->denominator == i)
  38. {
  39. int newFractionNumerator = current->numerator + current->next->numerator;
  40. int newFracTionDenominator = current->denominator + current->next->denominator;
  41. Fraction* newFraction = new Fraction(newFractionNumerator, newFracTionDenominator, current->next);
  42. current->next = newFraction;
  43. array[newFractionPosition++] = newFraction;
  44. }
  45. current = current->next;
  46. }
  47. }
  48. Fraction* current = array[0];
  49. while (current != NULL)
  50. {
  51. cout << current->numerator << " / " << current->denominator << endl;
  52. current = current->next;
  53. }
  54.  
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement