백준/실버

[백준 1735번] 분수 합 (C++)

게임개발기원 2025. 3. 27. 03:05

문제링크 : https://www.acmicpc.net/problem/1735

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int A1, B1, A2, B2;

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> A1 >> A2 >> B1 >> B2;

    int a = A1*B2 + B1*A2;
    int b = A2*B2;

    int tmp = gcd(a,b);
    cout << a/tmp << " " << b/tmp;

    return 0;
}

 

주어진 값에 따라 공통분모와 그에 대한 분수를 구해준다.

기약 분수를 구해야하기 때문에 구한 분모와 분수에 대해 최대공약수를 GCD를 통해 구해주고,

분모와 분수에 각각 나눠주면 된다.