anon20016

Дроби

Nov 25th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int gcd(int x, int y) {
  5. x = abs(x);
  6. y = abs(y);
  7. while (x != y) {
  8. if (x > y) {
  9. x -= y;
  10. }
  11. else {
  12. y -= x;
  13. }
  14. }
  15. return x;
  16. }
  17.  
  18. pair<int, int> relax(pair<int, int> a) {
  19. int g = gcd(a.first, a.second);
  20. a.first /= g;
  21. a.second /= g;
  22. return a;
  23. }
  24.  
  25. pair<int, int> sum(pair<int, int> a, pair<int, int> b) {
  26. return relax(make_pair(a.first * b.second + a.second * b.first, a.second * b.second));
  27. }
  28. pair<int, int> sub(pair<int, int> a, pair<int, int> b) {
  29. return relax(make_pair(a.first * b.second - a.second * b.first, a.second * b.second));
  30. }
  31. pair<int, int> mul(pair<int, int> a, pair<int, int> b) {
  32. return relax(make_pair(a.first * b.first, a.second * b.second));
  33. }
  34. pair<int, int> div(pair<int, int> a, pair<int, int> b) {
  35. return relax(make_pair(a.first * b.second, a.second * b.first));
  36. }
  37.  
  38. int main()
  39. {
  40.  
  41.  
  42. }
Add Comment
Please, Sign In to add comment