fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define el "\n"
  4. #define ll long long
  5. #define ull unsigned long long
  6. #define se second
  7. #define fi first
  8. #define be begin()
  9. #define en end()
  10. #define Faster cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
  11.  
  12. struct PhanSo
  13. {
  14. int tu, mau;
  15. };
  16.  
  17. PhanSo tong(const PhanSo& ps1, const PhanSo& ps2)
  18. {
  19. int tu, mau, tmp;
  20. PhanSo ans;
  21. tmp = ps1.mau /__gcd(ps1.mau, ps2.mau) * ps2.mau;
  22. ans.mau = tmp;
  23. ans.tu = ps1.tu*(tmp / ps1.mau) + ps2.tu*(tmp / ps2.mau);
  24. tmp = __gcd(ans.tu, ans.mau);
  25. ans.tu /= tmp;
  26. ans.mau /= tmp;
  27. return ans;
  28. }
  29.  
  30. bool ssLonHon(const PhanSo& ps1, const PhanSo& ps2)
  31. {
  32. double a = 1.0 * ps1.tu / ps1.mau;
  33. double b = 1.0 * ps2.tu / ps2.mau;
  34. if(a > b) return true;
  35. return false;
  36. }
  37. int main()
  38. {
  39. Faster;
  40. PhanSo a, b;
  41. cin >> a.tu >> a.mau >> b.tu >> b.mau;
  42. PhanSo x = tong(a, b);
  43. cout << x.tu << " " << x.mau << " ";
  44. if(ssLonHon(a, b)) cout << "true";
  45. else cout << "no";
  46. return 0;
  47. }
  48. /*
  49. #include <bits/stdc++.h>
  50. using namespace std;
  51.  
  52. struct PhanSo {
  53.   int tu, mau;
  54. };
  55.  
  56. void rutGon(PhanSo &ps) {
  57.   int gcd = __gcd(abs(ps.tu), abs(ps.mau));
  58.   ps.tu /= gcd;
  59.   ps.mau /= gcd;
  60.   if (ps.mau < 0) {
  61.   ps.tu = -ps.tu;
  62.   ps.mau = -ps.mau;
  63.   }
  64. }
  65.  
  66. PhanSo tong(const PhanSo &ps1, const PhanSo &ps2) {
  67.   PhanSo ans;
  68.   int lcm = ps1.mau / __gcd(ps1.mau, ps2.mau) * ps2.mau;
  69.   ans.tu = ps1.tu * (lcm / ps1.mau) + ps2.tu * (lcm / ps2.mau);
  70.   ans.mau = lcm;
  71.   rutGon(ans);
  72.   return ans;
  73. }
  74.  
  75. bool ssLonHon(const PhanSo &ps1, const PhanSo &ps2) {
  76.   int lcm = ps1.mau / __gcd(ps1.mau, ps2.mau) * ps2.mau;
  77.   int tu1 = ps1.tu * (lcm / ps1.mau);
  78.   int tu2 = ps2.tu * (lcm / ps2.mau);
  79.   return tu1 > tu2;
  80. }
  81.  
  82. int main() {
  83.   ios_base::sync_with_stdio(0);
  84.   cin.tie(0);
  85.   cout.tie(0);
  86.  
  87.   PhanSo a, b;
  88.   cin >> a.tu >> a.mau >> b.tu >> b.mau;
  89.  
  90.   rutGon(a);
  91.   rutGon(b);
  92.  
  93.   PhanSo x = tong(a, b);
  94.   cout << x.tu << " " << x.mau << " ";
  95.  
  96.   if (ssLonHon(a, b)) {
  97.   cout << "true";
  98.   } else {
  99.   cout << "false";
  100.   }
  101.  
  102.   return 0;
  103. }
  104. */
  105.  
Success #stdin #stdout 0s 5288KB
stdin
1 2 1 3 
stdout
5 6 true