본문 바로가기
코딩/문제풀이-백준

백준 1735번 - 분수 합 (C++)

by 남대현 2021. 5. 5.
반응형

문제

문제 풀이

두 분수를 더하고, 최소공약수를 구해주면 된다.

 

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
 
int gcd(int a, int b) 
    int r; 
    while (b != 0
    { 
        r = a % b; 
        a = b; 
        b = r; 
    } 
    return a; 
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    int a, s, d, f;
    cin >> a >> s >> d >> f;
    a *= f;
    d *= s;
    a += d;
    s *= f;
    int GCD = gcd(a, s);
    cout << a / GCD << ' ' << s / GCD << endl;
}
cs
반응형

댓글