Advertisement
pasholnahuy

Сложить 2 дроби

Jun 12th, 2023
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. using int64 = int64_t;
  3. using namespace std;
  4. int64 gcd(int64 a, int64 b){
  5.     while (a != 0 && b != 0){
  6.         if (a < b){
  7.             swap(a, b);
  8.         }
  9.         a = a % b;
  10.     }
  11.     return b;
  12. }
  13. int main(){
  14.     int64 a, b, c, d;
  15.     cin >> a >> b >> c >> d;
  16.     int GCD = gcd(d, b);
  17.     int lcm = d * b/GCD;
  18.     int num = (a*d + c*b)/GCD;
  19.     int t = gcd(num, lcm);
  20.     cout << num/t << " " << lcm/t;
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement