Advertisement
Schnuk

Untitled

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