알고리즘

C++ 알고리즘 - 백준 1735 분수 합

마루설아 2025. 1. 11. 23:42

https://www.acmicpc.net/problem/1735

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

void CPP_INIT() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

int main(void) {
	CPP_INIT();

	int n, m, x, y;
	int mo, ja;
	cin >> n >> m >> x >> y;

	mo = m * y;
	ja = (n * y) + (m * x);

	if (mo > ja) {
		for (int i = 2; i < ja;) {
			if (mo % i == 0 && ja % i == 0) {
				mo /= i;
				ja /= i;
			}

			else i++;
		}
	}

	else if (mo < ja) {
		for (int i = 2; i < mo;) {
			if (mo % i == 0 && ja % i == 0) {
				mo /= i;
				ja /= i;
			}

			else i++;
		}
	}

	else {
		cout << 1 << " " << 1;
		return 0;
	}

	cout << ja << " " << mo;
}