Advertisement
Schnuk

Untitled

Feb 23rd, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 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] = Fraction();
  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. int i = 0;
  49. Fraction current = array[0];
  50. while (current.next != NULL)
  51. {
  52. cout << current.numerator << " / " << current.denominator << endl;
  53. current = *current.next;
  54. }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement