백준 1735 c++

magicdrill·2024년 2월 29일
0

백준 문제풀이

목록 보기
64/654

백준 1735 c++

#include <iostream>

using namespace std;

int input(int lower, int upper);
void result(int A, int B, int C, int D, int* son, int* mother);
int find_GCM(int A, int B);
void irreducible_fraction(int* son, int* mother);

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

	int A, B, C, D, son, mother;

	A = input(1, 30000);
	B = input(1, 30000);
	C = input(1, 30000);
	D = input(1, 30000);
	result(A, B, C, D, &son, &mother);
	irreducible_fraction(&son, &mother);
	cout << son << " " << mother << "\n";

	return 0;
}

int input(int lower, int upper)
{
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

void result(int A, int B, int C, int D, int *son, int *mother)
{
	//분모는 최소공배수
	//분모 먼저 구하기 : 최소공배수 구하기
	int common_mother;
	int GCM;

	GCM = find_GCM(B, D);
	common_mother = (B * D) / GCM;
	*mother = common_mother;
	*son = (A * (common_mother / B)) + (C * (common_mother / D));

	return;
}

int find_GCM(int A, int B)
{
	int temp;

	if (A < B)
	{
		temp = B;
		B = A;
		A = temp;
	}
	else
	{
		;
	}
	while (B != 0)
	{
		temp = A % B;
		A = B;
		B = temp;
	}

	return A;
}

void irreducible_fraction(int *son, int *mother)
{
	int son_temp = *son, mother_temp = *mother;
	int GCM;

	//기약분수는 분자 분모에 최대공약수로 나눈 결과
	GCM = find_GCM(son_temp, mother_temp);
	*son = son_temp / GCM;
	*mother = mother_temp / GCM;

	return;
}

0개의 댓글